SELECT中具有相同OPTIONS(selectpicker)的不同值

时间:2018-03-01 18:13:59

标签: javascript jquery bootstrap-select bootstrap-selectpicker

我有两个具有相同值的SELECTS(带bootstrap-select)(例如GREEN,YELLOW,BLUE,WHITE)。但我不想在两个SELECTS上选择相同的值。如果我在第一个上有'蓝色',当我在第二个上选择蓝色时,第一个应该更改为默认“ - 请选择 - ”值。 有人能帮助我吗? TKS。

2 个答案:

答案 0 :(得分:0)

你可以通过简单地编写像这样的onChange事件

来做到这一点

function myFunction(el,el2) {
    if($("#"+el).val() == $("#"+el2).val()){
      $("#"+el).prop('selectedIndex', 0)
    };
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="mySelect1" onchange="myFunction('mySelect2','mySelect1')">
  <option value="">--Select--
  <option value="BMW">GREEN
  <option value="YELLOW">YELLOW
  <option value="BLUE">BLUE
  <option value="WHITE">WHITE
</select>

<select id="mySelect2" onchange="myFunction('mySelect1','mySelect2')">
   <option value="">--Select--
  <option value="BMW">GREEN
  <option value="YELLOW">YELLOW
  <option value="BLUE">BLUE
  <option value="WHITE">WHITE
</select>

答案 1 :(得分:0)

我很注意你的注意力。

但它并不适用于我的特定问题。

问题是我正在使用BOOTSTRAP-SELECT类。请查看(https://jsfiddle.net/WRobynson/v78o696j/11/)以了解。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>

<script>
function myFunction(el,el2) {
    if($("#"+el).val() == $("#"+el2).val()){
      $("#"+el).prop('selectedIndex', 0)
    };
}
</script>

<!-- HERE WORKS --><br>

 <select id="mySelect1" onchange="myFunction('mySelect2','mySelect1')">
  <option value="">--Select--
  <option value="BMW">GREEN
  <option value="YELLOW">YELLOW
  <option value="BLUE">BLUE
  <option value="WHITE">WHITE
</select>

<select id="mySelect2" onchange="myFunction('mySelect1','mySelect2')">
   <option value="">--Select--
  <option value="BMW">GREEN
  <option value="YELLOW">YELLOW
  <option value="BLUE">BLUE
  <option value="WHITE">WHITE
</select><br><br>


<!-- BUT HERE DOESN'T WORK -->


  <select id='mySelect3' class='selectpicker'  onChange="myFunction('mySelect4','mySelect3')">

   <option value="">--Select--
    <option value="BMW">GREEN
    <option value="YELLOW">YELLOW
    <option value="BLUE">BLUE
    <option value="WHITE">WHITE
    </select>


        <select id="mySelect4" class='selectpicker' onchange="myFunction('mySelect3','mySelect4')">

     <option value="">--Select--
  <option value="BMW">GREEN
  <option value="YELLOW">YELLOW
  <option value="BLUE">BLUE
  <option value="WHITE">WHITE

  </select>

韩国社交协会