假设你有2个“选择” HTML元素象下面这样:
的test.html
<html>
<head>
<script src="connectSelect.js"></script>
</head>
<body onload="connectSelect('first', 'second')">
<select id="first">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<select id="second">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</body>
</html>
只需使用对身体负荷传递的“选择”的ID已经取得“connectSelect”功能您想要连接的元素。
在标题中包含connectSelect.js文件。下面是该代码:
connectSelect.js
function connectSelect(id1, id2) {
var select1 = document.getElementById(id1);
var select2 = document.getElementById(id2);
var i, val1, text1, val2, text2, errText;
//to check whether both the select elements are of same length or not
if (select1.length != select2.length) {
alert("connectSelect Function Error: Both the 'Select' elements should have same number of options!");
return;
}
//after assuring both the select elements to be of same length
//to check whether both the select elements have same value and text
for (i = 0; i < select1.length; i++) {
val1 = select1.options[i].value;
text1 = select1.options[i].innerHTML;
val2 = select2.options[i].value;
text2 = select2.options[i].innerHTML;
if (val1 != val2 || text1 != text2) {
errText = "Both the 'Select' elements should have same options with same value and text!";
errText += "\n";
errText += "\n";
errText += "First mismatch: Option " + (i+1);
alert("connectSelect Function Error: " + errText);
return;
}
}
//after assuring both the select elements to be same
select1.addEventListener("change", function(){
var index = this.selectedIndex;
select2.options[index].selected = true;
});
select2.addEventListener("change", function(){
var index = this.selectedIndex;
select1.options[index].selected = true;
});
}
我已经插入了代码段也。尝试一下。
function connectSelect(id1, id2) {
\t var select1 = document.getElementById(id1);
\t var select2 = document.getElementById(id2);
\t var i, val1, text1, val2, text2, errText;
\t
\t //to check whether both the select elements are of same length or not
\t if (select1.length != select2.length) {
\t \t alert("connectSelect Function Error: Both the 'Select' elements should have same number of options!");
\t \t return;
\t }
\t
\t //after assuring both the select elements to be of same length
\t //to check whether both the select elements have same value and text
\t for (i = 0; i < select1.length; i++) {
\t \t val1 = select1.options[i].value;
\t \t text1 = select1.options[i].innerHTML;
\t \t val2 = select2.options[i].value;
\t \t text2 = select2.options[i].innerHTML;
\t \t
\t \t if (val1 != val2 || text1 != text2) {
\t \t \t errText = "Both the 'Select' elements should have same options with same value and text!";
\t \t \t errText += "\n";
\t \t \t errText += "\n";
\t \t \t errText += "First mismatch: Option " + (i+1);
\t \t \t alert("connectSelect Function Error: " + errText);
\t \t \t return;
\t \t }
\t }
\t
\t //after assuring both the select elements to be same
\t select1.addEventListener("change", function(){
\t \t var index = this.selectedIndex;
\t \t select2.options[index].selected = true;
\t });
\t select2.addEventListener("change", function(){
\t \t var index = this.selectedIndex;
\t \t select1.options[index].selected = true;
\t });
}
<html>
\t <head>
\t \t <script src="test.js"></script>
\t </head>
\t <body onload="connectSelect('first', 'second')">
\t \t <select id="first">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t \t <option value="2">2</option>
\t \t \t <option value="3">3</option>
\t \t \t <option value="4">4</option>
\t \t </select>
\t \t <select id="second">
\t \t \t <option value="0">0</option>
\t \t \t <option value="1">1</option>
\t \t \t <option value="2">2</option>
\t \t \t <option value="3">3</option>
\t \t \t <option value="4">4</option>
\t \t </select>
\t </body>
</html>
希望它帮助。
你可以使用jquery:attach回调来改变每个'
试试这个https://jsfiddle.net/jkhmvnuj/ –
太棒了!工作完美,谢谢你,你应该重新发布你的jsfiddle例子作为答案,所以我可以upvote你的答案是正确的,再次感谢。非常容易实现和少量的js代码。干杯。 –