gpt4 book ai didi

javascript - 错误 : Syntax error, 无法识别的表达式:#jquery

转载 作者:行者123 更新时间:2023-11-30 07:55:41 26 4
gpt4 key购买 nike

我有以下代码:

$('.questionAlternative').change(function() {
var nestedQuestion = $(this).attr('nestedQuestion');
if(nestedQuestion != null || nestedQuestion != "") {
$("#"+nestedQuestion).show();
} else {
$(this).parent().next('.nestedQuestionContainer').hide();
}
}

.questionAlternative 是两个单选按钮,是或否。

当我更改为否时,出现以下错误:

Error: Syntax error, unrecognized expression: #
throw new Error( "Syntax error, unrecognized expression: " + msg );

我不明白为什么。如果 nestedQuestion 为空,则不应进入 If 语句。但它似乎无论如何都在做?

最佳答案

你的条件不对。它表示“如果 nestedQuestion 不是 null OR nestedQuestion 不是 ””。 .."但 "" 不是 null,因此对于 "" 而言,条件为真。

将其更改为(&&):

if(nestedQuestion != null && nestedQuestion != "") {

或者只是使用 null"" 都是 falsy 的事实:

if(nestedQuestion) {

..实际上,attr 永远不会给你null。它只会给你一个字符串(可能是 "")或 undefined。 (但是 undefined == null 是真的,所以...)

关于javascript - 错误 : Syntax error, 无法识别的表达式:#jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40344670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com