【问题标题】:If I selected true value assign to other select option via jQuery如果我通过 jQuery 选择真值分配给其他选择选项
【发布时间】:2015-11-05 23:48:18
【问题描述】:

如果主题混淆了,我很抱歉。

我的网站上有两个选择。它使用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 或文本,但我需要不同的。

我该怎么做?

【问题讨论】:

    标签: javascript jquery select jquery-select2


    【解决方案1】:
    <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/

    【讨论】:

      【解决方案2】:

      您必须添加更改事件并根据从第一次选择 #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>

      【讨论】:

      • 感谢您的解决方案。我为我的结构改变了一点点,但在 if-else 条件下不起作用。请问你能检查一下吗? pastebin.com/WcGH4ffT
      • 不客气,检查控制台中是否有任何错误消息,是否可以为您的代码制作小提琴,以便我与您核实。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 2015-08-07
      • 2018-03-20
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多