Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多次运行jQuery函数_Jquery_Html_Checkbox - Fatal编程技术网

多次运行jQuery函数

多次运行jQuery函数,jquery,html,checkbox,Jquery,Html,Checkbox,我想在每次选中复选框时运行jQuery函数,目前它只在我第一次选中复选框时起作用,下一个选中的复选框将不起任何作用,依此类推 以下是我的jQuery: $('#check').change(function () { var price = $(this).data('price'); var currPrice = $('.pricedisplay').data('price'); $('.pricedisplay').text(price + currPrice);

我想在每次选中复选框时运行jQuery函数,目前它只在我第一次选中复选框时起作用,下一个选中的复选框将不起任何作用,依此类推

以下是我的jQuery:

$('#check').change(function () {
    var price = $(this).data('price');
    var currPrice = $('.pricedisplay').data('price');
    $('.pricedisplay').text(price + currPrice);
});
和一个复选框:

<input type="checkbox" id="check" name="check-1" data-price="50">

我最终会有数百个复选框,所以我不能为每个复选框创建单独的函数


提前感谢,我是jQuery的新手。

id
是唯一的,您可以使用类来代替:

<input type="checkbox" class="check" name="check-1" data-price="50">

id
是唯一的,您可以改用class:

<input type="checkbox" class="check" name="check-1" data-price="50">

id
是唯一的,您可以改用class:

<input type="checkbox" class="check" name="check-1" data-price="50">

id
是唯一的,您可以改用class:

<input type="checkbox" class="check" name="check-1" data-price="50">

您的问题是重复ID-页面上的每个ID都必须是唯一的。这将导致jQuery事件仅在其找到的第一个ID为的元素上触发


改用类,您的代码将运行良好。

您发布的是重复的ID-页面上的每个ID都必须是唯一的。这将导致jQuery事件仅在其找到的第一个ID为的元素上触发


改用类,您的代码将运行良好。

您发布的是重复的ID-页面上的每个ID都必须是唯一的。这将导致jQuery事件仅在其找到的第一个ID为的元素上触发


改用类,您的代码将运行良好。

您发布的是重复的ID-页面上的每个ID都必须是唯一的。这将导致jQuery事件仅在其找到的第一个ID为的元素上触发


改用类,您的代码就可以正常工作。

是否在每个复选框上使用相同的ID?您不应该这样做..您是否在每个复选框上使用相同的ID?您不应该这样做..您是否在每个复选框上使用相同的ID?您不应该这样做..您是否在每个复选框上使用相同的ID?你不应该那样做。。