如何纠正 jQuery 语法错误?
我正在尝试使用 jQuery 更新表单底部的 Total: (span.wrap p span
) 字段。
我有几个复选框,每个复选框都有一个包含在 title
属性中的关联价格,如下所示:title="$2"
。
我希望 Total: 字段在用户单击复选框时动态更新。这是我的脚本,但它抛出了如图所示的语法错误。
$("input:checkbox").toggle(
var v = $(this).attr("title").substr(-1) * 1; // syntax error here. subtstr() eliminates the $ sign
var t = $("span.wrap p span").text() * 1; // converts the string to a number
function () {
$("span.wrap p span").text(t + v);
},
function () {
$("span.wrap p span").text(t - v);
}
);
注意:总计字段的初始值为0
。有四个复选框;也许我需要使用each()?
关于如何实现这项工作有什么想法吗?
I'm trying to update a Total: (span.wrap p span
) field at the bottom of a form using jQuery.
I have a few checkboxes, each of which has an associated price contained within the title
attribute like so: title="$2"
.
I'd the Total: field to update dynamically when the user clicks a checkbox. Here is the script I have, but it's throwing a syntax error as shown.
$("input:checkbox").toggle(
var v = $(this).attr("title").substr(-1) * 1; // syntax error here. subtstr() eliminates the $ sign
var t = $("span.wrap p span").text() * 1; // converts the string to a number
function () {
$("span.wrap p span").text(t + v);
},
function () {
$("span.wrap p span").text(t - v);
}
);
Note: The total field has an initial value of 0
. There are four checkboxes; perhaps I need to use each()
?
Any ideas on how to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Toggle 接受一个函数
Toggle accepts a function
当您使用切换功能时,据我所知,您只能在两个功能块内定义代码。
When you use a toggle function, afaik you can only define code within the two function blocks.
改变:
到
change:
to