我有两个HTML选择元素:
<select name="businessName">
<option name="d3"> Car Ltd </option>
<option name="e11"> Food Business </option>
<option name="f2"> Builders </option>
</select>
<select name="businessCode">
<option name="d3"> d3 </option>
<option name="e11"> e11 </option>
<option name="f2"> f2 </option>
</select>
我如何使用JavaScript / jQuery,所以当我更改一个select时,另一个会改变? E.g:
答案 0 :(得分:3)
<强> jsBin demo 强>
您的options
应该具有 VALUE 属性(name
的实例):
<select name="businessName">
<option value="d3"> Car Ltd </option>
<option value="e11"> Food Business </option>
<option value="f2"> Builders </option>
</select>
<select name="businessCode">
<option value="d3"> d3 </option>
<option value="e11"> e11 </option>
<option value="f2"> f2 </option>
</select>
这就是你所需要的:
var $sel = $('[name=businessName], [name=businessCode]');
$sel.change(function(){
$sel.not(this).val( this.value );
});
答案 1 :(得分:1)
一种可能的方法:
var $businessNameSelect = $('[name=businessName]');
var $businessCodeSelect = $('[name=businessCode]');
$businessNameSelect.on('change', function() {
$businessCodeSelect[0].selectedIndex = this.selectedIndex;
});
$businessCodeSelect.on('change', function() {
$businessNameSelect[0].selectedIndex = this.selectedIndex;
});
Demo。关键是在selectedIndex
的{{1}}属性上运行。引用DOM Level 2 docs:
类型长的
HTMLSelectElement
所选选项的序数索引,从0开始。值 如果未选择任何元素,则返回-1。
答案 2 :(得分:1)
$("#bsname").change(function() {
$("#bscode")[0].selectedIndex = $(this)[0].selectedIndex;
});
$("#bscode").change(function() {
$("#bsname")[0].selectedIndex = $(this)[0].selectedIndex;
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="bsname" name="businessName">
<option name="d3"> Car Ltd </option>
<option name="e11"> Food Business </option>
<option name="f2"> Builders </option>
</select>
<select id="bscode" name="businessCode">
<option name="d3"> d3 </option>
<option name="e11"> e11 </option>
<option name="f2"> f2 </option>
</select>
&#13;
答案 3 :(得分:1)
我希望这是实际预期的结果:
$(".dittoSelect").change(function() {
if ($(this).attr("name") == "businessName") {
$("#businessCode option[name='" + $('option:selected', this).attr('name') + "']").attr("selected", "selected");
} else {
$("#businessName option[name='" + $('option:selected', this).attr('name') + "']").attr("selected", "selected");
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select name="businessName" id="businessName" class="dittoSelect">
<option name="d3">Car Ltd</option>
<option name="e11">Food Business</option>
<option name="f2">Builders</option>
</select>
<select name="businessCode" id="businessCode" class="dittoSelect">
<option name="d3">d3</option>
<option name="e11">e11</option>
<option name="f2">f2</option>
</select>
&#13;
答案 4 :(得分:0)
我可以看到一些jQuery方法,并认为提供一个不需要jQuery的不同方法是个好主意。
function Events(){
var selections = document.getElementsByClassName('selection');
// Add EventListeners to each element with the class off Selection
for(var i=0; i<selections.length; i++) {
selections[i].addEventListener("change",function(){PreSelect(this)},false);
}
}
function PreSelect(e){
var selections = document.getElementsByClassName('selection');
for(var i=0; i<selections.length; i++) {
//Set select to changed index
selections[i].selectedIndex=e.selectedIndex;
}
}
window.onload=Events;
&#13;
<select class="selection" name="businessName">
<option name="d3"> Car Ltd </option>
<option name="e11"> Food Business </option>
<option name="f2"> Builders </option>
</select>
<select class="selection" name="businessCode">
<option name="d3"> d3 </option>
<option name="e11"> e11 </option>
<option name="f2"> f2 </option>
</select>
&#13;