Javascript 错误:语法错误,无法识别的表达式:#jquery
我有以下代码:Javascript 错误:语法错误,无法识别的表达式:#jquery,javascript,jquery,Javascript,Jquery,我有以下代码: $('.questionAlternative').change(function() { var nestedQuestion = $(this).attr('nestedQuestion'); if(nestedQuestion != null || nestedQuestion != "") { $("#"+nestedQuestion).show(); } else { $(this).parent().next('
$('.questionAlternative').change(function() {
var nestedQuestion = $(this).attr('nestedQuestion');
if(nestedQuestion != null || nestedQuestion != "") {
$("#"+nestedQuestion).show();
} else {
$(this).parent().next('.nestedQuestionContainer').hide();
}
}
问题选项是两个单选按钮,是或否
当我更改为“否”时,会出现以下错误:
错误:语法错误,无法识别的表达式:#抛出新错误(“语法错误,无法识别的表达式:“+msg”)
我不明白为什么。如果nestedQuestion为空,则不应进入If语句。但看起来它还是这样做了?你的情况不对。它表示“如果
nestedQuestion
不是null
或nestedQuestion
不是”
。“但是”
不是null
,那么的条件为true
将其更改为和(&&
):
或者仅使用以下事实:null
和“
都是虚假的:
…实际上,attr
无论如何都不会给你null
。它只会给您一个字符串(可能是“”
)或未定义
。(但是undefined==null
是真的,所以…您的条件是错误的。它表示“如果nestedQuestion
不是null
或nestedQuestion
不是”
。“但是”
不是null
,那么的条件为true
将其更改为和(&&
):
或者仅使用以下事实:null
和“
都是虚假的:
…实际上,attr
无论如何都不会给你null
。它只会给您一个字符串(可能是“”
)或未定义
。(但是undefined==null
是真的,所以…!”空值
为真值
“”!=空的
是真的
啊,不知道“”不是空的。谢谢。@Bryan::-)这是其中之一,它们都是假的,但它们彼此之间不是==
。啊,不知道“”不是空的。谢谢。@Bryan::-)这是其中一件事,他们都是虚伪的,但他们彼此之间不是。
if(nestedQuestion != null && nestedQuestion != "") {
if(nestedQuestion) {