繁体   English   中英

如果我选择了真值,则通过jQuery分配给其他选择选项

[英]If I selected true value assign to other select option via jQuery

如果对主题感到困惑,对不起。

我的网站上有两个选择。 它使用select2插件。

首先选择:

<select name="" id="">
    <option value="one"></option>
    <option value="two"></option>
    <option value="three"></option>
    <option value="four"></option>
    <option value="five"></option>
</select>

还有一个:

<select name="" id="">
    <option value="other_one"></option>
    <option value="other_two"></option>
    <option value="other_three"></option>
    <option value="other_four"></option>
    <option value="other_five"></option>
</select>

他们有逻辑。 如果选中的值为one ,则其他选择中的值必须为other_one (并且应禁用其他选项)。 同样,其他选择选项将在这种情况下起作用。

我在https://select2.github.io/options.html#adapters和Stackoverflow上进行了研究,是的,我可以将触发器添加到选择中,例如打印html或文本,但是我需要使用其他触发器。

我该怎么做?

<select name="" id="select1">
<option value="one">one</option>
<option value="two">two</option>
<option value="three">three</option>
<option value="four">four</option>
<option value="five">five</option>

<select name="" id="select2">
    <option value="other_one">other_one</option>
    <option value="other_two">other_two</option>
    <option value="other_three">other_three</option>
    <option value="other_four">other_four</option>
    <option value="other_five">other_five</option>
</select>

$('select').select2();

$("#select1").change(function()
{   
    $('#select2 option').prop("disabled",false); // remove disabled on all options - basically do a reset
    $('#select2 option:selected').prop("disabled",true); // disable the current selected option
   $('#select2').select2().select2('val', "other_" + $(this).val()); // set the value of #select2 by concatenating the value from #select1
    $('#select2 option:not(:selected)').prop("disabled",true); // disable all other non-selected options
});

http://jsfiddle.net/o5y9959b/10/

您必须添加更改事件,并根据第一个选择#select1选择的一个来更改第二个选择#select2的值。

希望这可以帮助。

 $("#select1, #select2").select2(); $("#select1").on('change',function() { $('#select2 option').removeAttr('disabled'); var selected_1_value = $(this).val(); $("#select2").val('other_'+selected_1_value).change(); $("#select2 option:not(:selected)").attr('disabled', 'disabled'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <link href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css" rel="stylesheet"/> <script src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js"></script> <select name="" id="select1"> <option value="one">1</option> <option value="two">2</option> <option value="three">3</option> <option value="four">4</option> <option value="five">5</option> </select> <select name="" id="select2"> <option value="other_one">other 1</option> <option value="other_two">other 2</option> <option value="other_three">other 3</option> <option value="other_four">other 4</option> <option value="other_five">other 5</option> </select> 

暂无
暂无

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

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