我可以在使用jQuery的if语句中有多个条件吗?以下代码根本没有捕获错误。 根据发布的建议进行了编辑
$('.goRedIMG').on('click',function(event){
var ischecked = false;
var isOKtoSubmit = true;
var alertMessage = 'No tools have been selected';
var statusvar = '';
var transferstatusvar = '';
var action = $('#uTransaction option:selected').html();
$('.chkaction').each(function() { //loop through each checkbox
statusvar = $(this).closest('tr').children('.recordStatus').html();
transferstatusvar = $(this).closest('tr').children('.transferstat').html()
if($(this).prop('checked')) {
ischecked = true;
if((action == 'Transfer' && statusvar != 'ACTIVE') || (transferstatusvar != 'OK')){
isOKtoSubmit = false;
alertMessage = 'One or more records cannot be transferred because status is not ACTIVE and transfer status is not OK';
}
}
if(action == 'Accept Transfer' && transferstatusvar != 'AWAITING'){
isOKtoSubmit = false;
alertMessage = 'One or more records cannot be accepted or rejected because status is not ACTIVE or Transfer Status is not AWAITING';
}
});
if(isOKtoSubmit && ischecked !== false && action !== '--Select One--'){
$('#toolActions').submit();
}else {
alert(alertMessage);
}
});
答案 0 :(得分:2)
如果您在同一if参数中使用&&
(AND)和||
(OR),则需要对它们进行分组。
所以不要这样:
if(action == 'Transfer' && statusvar !== 'ACTIVE' || (transferstatusvar !== 'OK'))
你应该这样做:
if((action == 'Transfer' && statusvar != 'ACTIVE') || (transferstatusvar != 'OK'))
(另外,如果您想检查是否不等于,则不需要!==
,而是执行!=
)