简体   繁体   English

jQuery选择框增加值,仅目标下一个选择

[英]jQuery select box increase value, only target next select

I have a simple script that increases and decreases the selected values of a select box: 我有一个简单的脚本,可以增加和减少选择框的选定值:

$(".inc").click(function(){
    $("select option:selected").next().prop("selected", true);
});
$(".dec").click(function(){
    $("select option:selected").prev().prop("selected", true);
});

However, I have 20 selects in one form, and dont want to have to repeat the script with new #ids to only associate the script with the next select box (eg select#1, sesect#2 ....) 但是,我以一种形式有20个选择,并且不想用新的#id重复脚本以仅将脚本与下一个选择框关联(例如select#1,sesect#2 ....)。

At present, when the user clicks inc or dec on any select box, ALL select values in the form are updated, not just one. 当前,当用户单击任何选择框上的inc或dec时,表单中的所有选择值都会更新,而不仅仅是一个。

Anyone know how to target only the next, immediate select box when the script is executed? 有谁知道在执行脚本时如何仅将下一个立即选择框作为目标?

Thanks! 谢谢!

Where is .inc in relation to the select menu? 与选择菜单有关的.inc在哪里? Is there 20 inc's as well as 20 select menus? 是否有20个公司以及20个选择菜单?

If the structure is: 如果结构为:

<div class="inc">increment</div>
<select><option></option><option></option></select>

You should be able to use 您应该可以使用

$(".inc").click(function(){
$(this).next('select > option:selected').next().prop("selected", true);
});

Updated: Note that use of .next() depends on your HTML structure. 更新:请注意,.next()的使用取决于您的HTML结构。 Using siblings() is properly better. 使用siblings()会更好。 See here for an example: http://jsfiddle.net/JZJZh/ 参见此处的示例: http : //jsfiddle.net/JZJZh/

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

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