jQuery - 条件中的语法错误
$(".editAd").change({
if ($(this).val() == 1)
{
$("#submitBtn").val("Next Step");
}
else
{
$("#submitBtn").val("Submit Changes");
}
});
该代码的第二行有语法错误。缺少:房产 ID 后。我发誓这是在条件中正确使用 val() ,但我想我可能是错的......
我知道这是一个非常简单的修复,但我在几分钟的研究中找不到任何资源,我希望 SO 的用户可以帮助我。 :)
谢谢你!
编辑:开发人员总是会犯一个愚蠢的错误,让他们困扰一段时间......始终检查整个代码是否有错误,因为开发人员工具实际上可能无法拾取正确的错误行,就像在本例中一样。
$(".editAd").change({
if ($(this).val() == 1)
{
$("#submitBtn").val("Next Step");
}
else
{
$("#submitBtn").val("Submit Changes");
}
});
There is a syntax error on the second line of this code. Missing : after property ID. I swore this was the proper use of val() in a conditional, but I suppose I could be wrong...
I know this is a pretty simple fix, but I can't find any resources off of a few minutes of research, and I was hoping the users of SO could help me out. :)
Thank you!
EDIT: There's always a silly error that developers make that stumps them for a period of time... Always review the ENTIRETY of your code for errors because the developer tools may not actually pick up the proper line of the error, as in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您忘记添加 function(){}
You forgot to include function(){}
val()
是一个函数执行。要设置值,请执行类似
$('selector').val('set me');
而不是
$('selector').val() = 'set me';< /code>
从这里开始。其他事情可能会澄清。
val()
is a function execution.to set a value do something like
$('selector').val('set me');
not
$('selector').val() = 'set me';
start there. Other things might clear up.
您的代码存在很多问题。首先,您所说的错误的原因是缺少
function
关键字。这是必需的,因为change
方法采用函数作为参数。接下来,您要为
val
方法分配一个值,这是不正确的 ($("#submitBtn").val() = "Next Step";
)。您需要将字符串作为参数传递。第三,您的
else
块非常混乱。我不确定你想在那里做什么,但它会评估为类似true.val() = "Submit Changes";
这是完全错误的。这是对函数的另一个赋值,并且您尝试在布尔值而不是 jQuery 对象上调用该函数。因此,基于所有这些,我认为这就是您想要做的事情:
There are numerous problems with your code. Firstly, the cause of the error you are talking about, is the missing
function
keyword. That's required as thechange
method takes a function as an argument.Next, you're assigning a value to the
val
method, which is incorrect ($("#submitBtn").val() = "Next Step";
). You need to pass the string in as an argument.Third, your
else
block is very confusing. I'm not sure what you were trying to do there, but it will evaluate to something liketrue.val() = "Submit Changes";
which is completely wrong. It's another assignment to a function, and you're trying to call that function on a boolean value, not a jQuery object.So, based on all that, this is what I think you were trying to do:
属性 ID 后缺少:
这是由于条件之前缺少
function ()
标识符造成的。因为它被括在{}
中,所以 javascript 解析器认为您正在传递一个带有if
的对象文字作为属性标识符。它正在寻找后面的冒号来标识该财产的价值。Not sure what you are trying to do on this line:
但看起来您正在尝试对布尔值调用
val
,这是无效的。您还将一个字符串分配给val()
的输出,但这是行不通的。为了给元素赋值,jQuery 的 val 采用一个参数:
Missing : after property ID
This is caused by the lack of a
function ()
identifier before the conditional. Because it was bracketed{}
the javascript parser thought you were passing an object literal withif
as a property identifier. It was looking for a colon after it to identify the value of the property.Not sure what you are trying to do on this line:
But it looks like you are trying to call
val
on a boolean value, which is not valid. You are also assigning a string to the output ofval()
which won't work.To assign a value to an element, jQuery's val takes one parameter: