存在元件I具有的代码块,看起来像这样:检查是否与jQuery
//Account Number
account = $(msg).find('[name=account_number]').val();
if(account){
$('#progressUpdates').append('<span class="glyphicon glyphicon-ok"></span> Found Account Number<br />');
}else{
$('#progressUpdates').append('<span class="glyphicon glyphicon-remove"></span> Account Number not found<br />');
}
的问题是,如果被分配到帐户中的元件不存在,它会引发错误。我试着做:
if(account.length){}
但问题不在于if语句,它的赋值变量。有更好的解决方案吗?
当前的代码应该工作。 'val()'返回'undefined'是它所调用的元素不存在。的 – techfoobar
可能重复[是否有一个“存在”功能的jQuery?](http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery) – undefined
你尝试过的typeof帐户! =='未定义'? – euther