2017-01-03 111 views
0

我有两个相同的选择与不同的ID。如果我改变另一个应改变为相同的选项。使用Jquery。根据其他选择的手动更改自动更改选定的选项

<select name="quantityMobile" id="quantityMobile"> 

    <option selected value="1">1 pice</option> 
    <option value="2">2 pice</option> 
    <option value="3">3 pice</option> 
    <option value="4">4 pice</option> 

</select> 

<select name="quantityTablet" id="quantityTablet"> 

    <option selected value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
    <option value="4">4</option> 

</select> 
+1

...和http://stackoverflow.com/questions/36457921/how-to-change-select-tag-value-when-other-select-is-change和其他几个。发布前请彻底搜索。更多关于搜索[这里](/帮助/搜索)。 –

回答

1

使用更改事件处理程序并基于该值进行更新。

var $ele = $('#quantityTablet,#quantityMobile').change(function() { 
 
    $ele.val(this.value); 
 
    // or $ele.not(this).val(this.value); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select name="quantityMobile" id="quantityMobile"> 
 
    <option selected value="1">1 pice</option> 
 
    <option value="2">2 pice</option> 
 
    <option value="3">3 pice</option> 
 
    <option value="4">4 pice</option> 
 
</select> 
 

 
<select name="quantityTablet" id="quantityTablet"> 
 
    <option selected value="1">1</option> 
 
    <option value="2">2</option> 
 
    <option value="3">3</option> 
 
    <option value="4">4</option> 
 
</select>

+0

但它并没有与selectboxIt一起工作......你也有问题吗? –

+0

@NiklasStadler:你可以在http://jsfiddle.net分享代码吗 –