Jquery:每次更改的操作 [英] Jquery: Action for each change

查看:78
本文介绍了Jquery:每次更改的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续: https:// stackoverflow .com / questions / 16649933 / jquery-on-form-change-with-bootstrap-slider / 16650319?noredirect = 1

我目前正在使用下面是捕获滑块的ID并更改#log的内容。这个问题是只捕获第一个滑块。我需要在更改任何滑块时执行此操作。

I am currently using the below to capture the ID of a slider and change the content of #log. The problem with this is that is only capture the first slider. I need this to be actioned when any slider is changed.

我尝试在所有滑块上使用价格ID,但这不起作用。

I've tried using the ID of price on all my sliders, however this doesn't work.

$('#price').each(function(index,value){
    $(this).bind('slide', function(){
        $("#log").prepend('<p>Changed</p>')
    });
});


推荐答案

这不起作用的原因是因为JavaScript符合使用HTML规范,HTML规范声明 ID属性必须是唯一的

The reason this doesn't work is because JavaScript conforms with the HTML specification, and the HTML specification states that ID attributes must be unique.

您不能拥有 id =price<的两个元素/ code>。因为这个JavaScript会在找到第一个匹配后停止搜索。你的 .each()循环只会出现一次,无论在第一次出现多少 #price 元素之后。

You aren't allowed to have two elements with id="price". Because of this JavaScript will stop searching after it has found the first match. Your .each() loop will only go around once, regardless of how many #price elements are present after the first.

要解决此问题,请改用类。例如:

To resolve this, use classes instead. For example:

<div class="price">...</div>
<div class="price">...</div>
<div class="price">...</div>



$('.price').each(function(index,value) { ... });

您还需要更改 #log 要使用类的元素。

You will also need to change your #log elements to use classes, too.

这篇关于Jquery:每次更改的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆