简体   繁体   English

从选择框中选择一个值并使用 jquery 从另一个选择框中自动选择一个值

[英]Select a value from a select box and auto select a value from another select box using jquery

I have two select boxes.我有两个选择框。 The first select box has the id #first and the second select box has the id #second.第一个选择框的id为#first,第二个选择框的id为#second。

Each select box has two options with values 1,2.每个选择框都有两个选项,值为 1,2。 I want when i select the first option from the first select box to auto select the second option from the second select box AND when i select the second option from the first box to auto select the first option of the second select box:我希望当我从第一个选择框中选择第一个选项时自动从第二个选择框中选择第二个选项,当我从第一个框中选择第二个选项时自动选择第二个选择框中的第一个选项:

Here is my code:这是我的代码:

 $(document).ready(function() { $('#first').change(function() { var val = $('#first').prop('selectedIndex'); $("#second").prop('selectedIndex', val); if (val == "1") { $("#second").prop('selectedIndex', val == "2"); } else if (val == "2") { $("#second").prop('selectedIndex', val == "1"); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <select name="first" style="cursor:pointer;" id="first" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">First select</option> <option value="1" style="color:#000;">I want an apple</option> <option value="2" style="color:#000;">I want an orange</option> </select> <select name="second" style="cursor:pointer;" id="second" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">Second select</option> <option value="1" style="color:#000;">You got an orange</option> <option value="2" style="color:#000;">You got an apple</option> </select>

You can set a select by its value using .val() , in your case as you only have 2 values you can set the new value via the option value= instead of its selectedIndex using:您可以使用.val()通过其值设置select ,在您的情况下,因为您只有 2 个值,您可以通过option value=而不是它的selectedIndex设置新值:

$("#second").val(val == 1 ? 2 : 1);

Updated snippet:更新的片段:

 $(document).ready(function() { $('#first').change(function() { var val = $('#first').val() * 1; $("#second").val(val == 1 ? 2 : 1); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <select name="first" style="cursor:pointer;" id="first" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">First select</option> <option value="1" style="color:#000;">I want an apple</option> <option value="2" style="color:#000;">I want an orange</option> </select> <select name="second" style="cursor:pointer;" id="second" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">Second select</option> <option value="1" style="color:#000;">You got an orange</option> <option value="2" style="color:#000;">You got an apple</option> </select>


For completeness, to keep using selectedIndex (which is subtly different from value= ), it's very similar as the option values are the same as the index, if they're not then you would need to change the values in the above, but not if using selectedIndex - conversely, if you change the order of the option s then using value will still work while using selectedIndex would stop working.为了完整起见,要继续使用selectedIndex (与value=略有不同),它非常相似,因为option值与索引相同,如果不是,则您需要更改上面的值,但不是如果使用 selectedIndex - 相反,如果您更改option的顺序,则使用 value 仍然有效,而使用 selectedIndex 将停止工作。

 $(document).ready(function() { $('#first').change(function() { var val = $('#first').prop('selectedIndex'); $("#second").prop('selectedIndex', val === 1 ? 2 : 1); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <select name="first" style="cursor:pointer;" id="first" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">First select</option> <option value="1" style="color:#000;">I want an apple</option> <option value="2" style="color:#000;">I want an orange</option> </select> <select name="second" style="cursor:pointer;" id="second" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">Second select</option> <option value="1" style="color:#000;">You got an orange</option> <option value="2" style="color:#000;">You got an apple</option> </select>

It will be much easier to make the options on the same index.在同一索引上创建选项会容易得多。 So apple will be first in the two selects, and orange will be the second in both, so on...所以苹果将是两个选择中的第一个,橙色将是两个选择中的第二个,依此类推......

 $(document).ready(function() { $('#first').change(function() { $("#second").prop('selectedIndex', $('#first').prop('selectedIndex')); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <select name="first" style="cursor:pointer;" id="first" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">First select</option> <option value="1" style="color:#000;">I want an apple</option> <option value="2" style="color:#000;">I want an orange</option> </select> <select name="second" style="cursor:pointer;" id="second" placeholder="Please select"> <option value="" disabled="" selected="" hidden="">Second select</option> <option value="1" style="color:#000;">You got an apple</option> <option value="2" style="color:#000;">You got an orange</option> </select>

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

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