最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if variable is greater than something but not undefined - Stack Overflow

programmeradmin3浏览0评论
var ship = $("input[name=shippingamt]").length;
var counting1 = $(".submit-btn-1 li.checked:not(.title)").length;

if ((counting1 == 6)&&(ship >= 1)&&($('input[name=ideclare1]').is(":checked"))){

The above snippet is what I have so far, pretty much I have a 3 essential fields, a variable called Ship, an checklist called counting1 and a checkbox check.

But if Ship and Checklist are fine and the checkbox called ideclare1 is checked and unchecked then the value ship goes to undefined and is still class as valid as the length is >= 1.

Can any suggest how to check if a field is defined and has a length greater than 1?

Regards,

Donald

var ship = $("input[name=shippingamt]").length;
var counting1 = $(".submit-btn-1 li.checked:not(.title)").length;

if ((counting1 == 6)&&(ship >= 1)&&($('input[name=ideclare1]').is(":checked"))){

The above snippet is what I have so far, pretty much I have a 3 essential fields, a variable called Ship, an checklist called counting1 and a checkbox check.

But if Ship and Checklist are fine and the checkbox called ideclare1 is checked and unchecked then the value ship goes to undefined and is still class as valid as the length is >= 1.

Can any suggest how to check if a field is defined and has a length greater than 1?

Regards,

Donald

Share Improve this question edited Jul 11, 2012 at 15:41 ngplayground asked Jul 11, 2012 at 15:23 ngplaygroundngplayground 21.7k37 gold badges98 silver badges175 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

Check the .length of the jQuery object.

var ideclare = $('input[name=ideclare1]');

if ((counting1 == 6)&&(ship >= 1)&&(ideclare.length > 0 && ideclare.is(":checked"))){

edit well if you're just checking a regular variable, just pare it to undefined:

if (counting1 == 6 && (ship !== undefined && ship >= 1) ...
发布评论

评论列表(0)

  1. 暂无评论