我有2个选择列表mySelect和mySelect2.如果单击复选框,则选择一个时,另一个同时更改.
请检查以下代码:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function displayResult() {
if(document.form1.billingtoo.checked == true) {
var x = document.getElementById("mySelect").selectedIndex;
// Set selected index for mySelect2
document.getElementById("mySelect2").selectedIndex = x;
}
}
</script>
</head>
<body>
<form name="form1">
Select your favorite fruit:
<select id="mySelect">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
<br>
<input type="checkbox" name="billingtoo" onclick="displayResult()">
<br>
Select your favorite fruit 2:
<select id="mySelect2">
<option>Apple</option>
<option>Orange</option>
<option>Pineapple</option>
<option>Banana</option>
</select>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
...如何禁用(灰显)mySelect2,以便在值到位后不能对任何更改进行任何更改?
谢谢.
不确定为什么你的问题被标记为"jQuery"当你没有使用任何,但仍然:
// with "plain" JS:
document.getElementById("mySelect2").disabled = true;
// with jQuery
$("#mySelect2").prop("disabled", true);
Run Code Online (Sandbox Code Playgroud)
无论哪种方式设置disabled返回以false重新启用控件.
以下是使用jQuery重写函数的众多方法之一:
$(document).ready(function() {
// bind the checkbox click handler here,
// not in an inline onclick attribute:
$('input[name="billingtoo"]').click(function() {
var $mySelect2 = $("#mySelect2");
// if checkbox is checked set the second select value to
// the same as the first
if (this.checked)
$mySelect2.val( $("#mySelect").val() );
// disable or re-enable select according to state of checkbox:
$mySelect2.prop("disabled", this.checked);
});
});
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/nnnnnn/99TsC/
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |