简体   繁体   English

jQuery更改多个选定元素

[英]jQuery on change multiple selected elements

I have multiple select elements like this 我有多个这样的选择元素

<select id="changefilter" name="id"><option value="">1</option></select>
<select id="changefilter" name="price"><option value="">1</option></select>
<select id="changefilter" name="sort"><option value="">1</option></select>

I then have a jquery on change function that listens for changes made to changefilter and then submits form applyfilter like this: 然后,我有一个关于change函数的jQuery,它侦听对changefilter所做的更改,然后提交如下形式的applyfilter:

jQuery('#changefilter').change(function () {
    jQuery('#applyfilter').submit();
});

Problem is that it only seems to apply apply the onchange form submit to the first field, I would like this to apply to any select fields that have changed. 问题在于,似乎仅将onchange表单提交应用于第一个字段,我希望将其应用于已更改的所有选择字段。

Rather than giving each select field a unique name and have different .change events, is there a way to have the one code listen to all the select elements? 除了给每个选择字段一个唯一的名称并具有不同的.change事件外,还有没有办法让一个代码侦听所有选择元素?

id ( # ) must be unique use clasess ( . ) instead id( #必须是唯一的,请使用clases( . )代替

<select class="changefilter" name="id"><option value="">1</option></select>
<select class="changefilter" name="price"><option value="">1</option></select>
<select class="changefilter" name="sort"><option value="">1</option></select>

jQuery('.changefilter').change(function () {
    jQuery('#applyfilter').submit();
});

Id has to be unique for each of the element. 每个元素的ID必须唯一。 Class, on the other hand, can be multiple 另一方面,类可以是多个

将一个类添加到多重选择并执行$('。classname')。change

Change id to class 更改班级编号

<select class="changefilter" name="id"><option value="">1</option></select>
<select class="changefilter" name="price"><option value="">1</option>   </select>
<select class="changefilter" name="sort"><option value="">1</option></select>

jQuery('.changefilter').change(function () {
jQuery('#applyfilter').submit();
});

Please change the id attribute to class attribute, since id has to be unique, you can't have several DOM elements with the same id. 请将id属性更改为class属性,因为id必须是唯一的,所以不能有多个具有相同id的DOM元素。 If you will change your selector from jQuery('#changefilter') to jQuery('.changefilter') it will work as expected 如果将选择器从jQuery('#changefilter')更改为jQuery('.changefilter') ,它将按预期工作

Use classes instead of ids and here a exemple how to get name and value of what is selected. 使用类而不是ID,这里是一个示例,说明如何获取所选内容的名称和值。

<select class="changefilter" name="id"><option value="1">1</option><option value="2">2</option></select>
<select class="changefilter" name="price"><option value="1">1</option><option value="2">2</option></select>
<select class="changefilter" name="sort"><option value="1">1</option><option value="2">2</option></select>

<script>
    $('.changefilter').change(function () {
        alert('' + this.name + ' is change to ' + this.value)
    });
</script>

ID should be unique. ID应该是唯一的。 Base on your case, you must change the ID. 根据您的情况,您必须更改ID。

<select id="id" name="id"><option value="">1</option></select>
<select id="price" name="price"><option value="">1</option></select>
<select id="sort" name="sort"><option value="">1</option></select>

Then ids should be seperated by coma. 然后,id应以逗号分隔。

.change( should be like: .change(应该像这样:

jQuery('#id,#price,#sort').change(function () {
    jQuery('#applyfilter').submit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM