我有以下内容:
var detail = 'two';
function chkDetail(detail){
if(detail == 'one') {
jQuery('#valMsg').text('one');
}
if(detail == 'two') {
jQuery('#valMsg').text('two');
}
if(detail == 'three') {
jQuery('#valMsg').text('three');
}
else {
jQuery('#valMsg').text('NO');
}
}
根据要将什么字符串传递到函数中,我希望将适当的消息传递到valMsg
div中。
在上述情况下,我的函数不断返回NO
。
谁能告诉我为什么呢?
是if
陈述不正确的?
您已经分开了条件。我将添加空格来说明:
// first condition
if(detail == 'one') {
jQuery('#valMsg').text('one');
}
// second condition
if(detail == 'two') {
jQuery('#valMsg').text('two');
}
// third condition
if(detail == 'three') {
jQuery('#valMsg').text('three');
}
else {
jQuery('#valMsg').text('NO');
}
因此,例如,如果detail
为“ one”,则:
if
块if
阻止if
块,而是进入else
块(因为else
是的逻辑逆if
)如果所有条件都相同,请使用else if
块:
// all one condition
if(detail == 'one') {
jQuery('#valMsg').text('one');
}
else if(detail == 'two') {
jQuery('#valMsg').text('two');
}
else if(detail == 'three') {
jQuery('#valMsg').text('three');
}
else {
jQuery('#valMsg').text('NO');
}
这样,一旦输入了一个程序段,其余的就会被跳过,因为整体上已经满足了条件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句