如何纠正 jQuery 语法错误?

发布于 2024-08-11 05:32:18 字数 713 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妞丶爷亲个 2024-08-18 05:32:18

Toggle 接受一个函数

$("input:checkbox").toggle(function() {
   var v...

Toggle accepts a function

$("input:checkbox").toggle(function() {
   var v...
渔村楼浪 2024-08-18 05:32:18

当您使用切换功能时,据我所知,您只能在两个功能块内定义代码。

When you use a toggle function, afaik you can only define code within the two function blocks.

对你的占有欲 2024-08-18 05:32:18

改变:

var v = $(this).attr("title").substr(-1) * 1

var v = $(this).attr("title").substr(1) * 1

change:

var v = $(this).attr("title").substr(-1) * 1

to

var v = $(this).attr("title").substr(1) * 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文