简体   繁体   English

如何使用select2从多个选择框中选择默认选项

[英]How to select default options from multiple select boxes using select2

In my code there are three different select box. 在我的代码中,有三个不同的选择框。 I would like that when a selectbox is clicked, the others receive a default value so that only one select box is active. 我希望单击一个选择框时,其他选择框会收到一个默认值,因此只有一个选择框处于活动状态。

<select class="form-control select2" style="width: 100%;" id="seleciona_combo">
    <option value="a">Select food</option>
    <option>aba</option>
</select>

<select disabled class="form-control select2" style="width: 100%;" id="seleciona_Produto">
    <option value="b">Select drink</option>
</select>

<select class="form-control select2" style="width: 100%;" id="seleciona_adicionais">
    <option value="c">Select add</option>
    <option>oi</option>
</select>

Javascript: 使用Javascript:

    $("#seleciona_combo").on("change", function(){
        $("#seleciona_Produto").select2().select2('val','b');
        $("#seleciona_adicionais").select2().select2('val','c');
    });

    $("#seleciona_adicionais").on("change", function(){
        $("#seleciona_combo").select2().select2('val','a');
        $("#seleciona_adicionais").select2().select2('val','c');
    });

    $("#seleciona_adicionais").on("change", function(){
        $("#seleciona_Produto").select2().select2('val','b');
        $("#seleciona_combo").select2().select2('val','a');
    });

Assuming you mean the 1st option as the "default" try this: 假设您将第一个选项设为“默认”,请尝试以下操作:

$('.form-control').on("change", function(){
  var $var = $(this)
  $('.form-control').each( function(){
    if( $var != $(this) )
       $(this).selectedIndex = 0
  })

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

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