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

标签 javascript jquery select jquery-select2

如果主题感到困惑,我很抱歉。

我的网站上有两个选择。它使用 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/

关于javascript - 如果我选择了 true 值,则通过 jQuery 分配给其他选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33556987/

相关文章:

javascript - readPixels 性能,是什么让它这么慢?

javascript - 为什么将 Window 对象传递给函数

javascript - 如何使用jquery将html对象连接到html字符串?

iphone - 如何使用 NSFetchedRequest 或 NSPredicate 从实体中选择所有行?

javascript - 在旧浏览器中显示表情符号

javascript - Aurelia 中 View 模型之间的接口(interface)

javascript - 将调用者传递给允许我使用 .attr 方法的 javascript 函数

jquery - 单击这个或那个,然后执行某些操作

javascript - 使用Dalekjs测试工具,当Option标签中没有 "value"属性时,如何在Dropdown(选择元素)中选择Option?

php - 选择每个成员的最后一条消息