Javascript 使用复选框重置下拉列表
目前,我有一个复选框,如果不选中,下拉列表将被禁用。取消选中时,我可以添加哪一行来重置下拉列表Javascript 使用复选框重置下拉列表,javascript,checkbox,Javascript,Checkbox,目前,我有一个复选框,如果不选中,下拉列表将被禁用。取消选中时,我可以添加哪一行来重置下拉列表 <body onload="enable_dropdown(false);"> <form name="f1" method="post" onload="enable_dropdown(false);"> <input type="checkbox" name="others" onclick="enable_dropdown(this.checked)" &
<body onload="enable_dropdown(false);">
<form name="f1" method="post" onload="enable_dropdown(false);">
<input type="checkbox" name="others" onclick="enable_dropdown(this.checked)" >Others
<select id="that_select">
<option value="a">A</option>
<option value="b" selected>B</option>
<option value="c">C</option>
</select></form>
试试这个:
function enable_dropdown(inputStatus)
{
var selectElement = document.getElementById("that_select");
if(inputStatus == true)
{
// $(selectElement).removeAttr("disabled");
selectElement.removeAttribute("disabled");
}
else
{
// $(selectElement).attr('disabled', 'disabled');
selectElement.setAttribute("disabled","disabled");
}
// Then reset the select's selected index to clear current selection
selecElement.selectedIndex = -1;
}
给你:
<body onload="enable_dropdown(false);">
<form name="f1" method="post" onload="enable_dropdown(false);">
其他
<select id="that_select">
<option value="a">A</option>
<option value="b" selected>B</option>
<option value="c">C</option>
</select></form>
function enable_dropdown(status)
{
//status=!status;
var sel=document.getElementById('that_select');//document.f1.that_select.disabled
if(status||false) sel.removeAttribute('disabled');
else sel.setAttribute('disabled','disabled');
}
A.
B
C
功能启用下拉列表(状态)
{
//状态=!状态;
var sel=document.getElementById('that_select');//document.f1.that_select.disabled
如果(状态| | false)选择删除属性(“禁用”);
else sel.setAttribute('disabled','disabled');
}
使用jQuery查找以下代码
HTML
取决于“重置下拉列表”的含义。我知道你希望它重新选择初始值,对吗 最简单的解决方案是将此添加到js中:
document.f1.that_select.value = "b";
更优雅的解决方案:
<form name="f1" method="post" onload="enable_dropdown(false);">
<input type="checkbox" name="others" onclick="enable_dropdown(this.checked)" >Others
<select id="that_select">
<option value="a">A</option>
<option value="b" selected data-default="true">B</option>
<option value="c">C</option>
</select>
</form>
其他
A.
B
C
JS:
功能启用下拉列表(状态)
{
var select=document.f1.\u选择的文件;
状态=!状态;
选择。禁用=状态;
对于(变量i=0;i
功能启用下拉列表(b){
变量b=!b;
如果(b){
document.f1.选择.setAttribute(“禁用”,b);
}否则{
document.f1.选择.removeAttribute(“禁用”);
}
}
其他
A.
B
C
使用JQuery要容易得多
纯JavaScript是必需的,或者jQuery也很好?我想工作很好::这正是我所做的。我花了一分钟才意识到我能做到。
$('#chk').change(function(){
if($('#chk').is(':checked')){
$('#that_select').removeAttr('disabled');
}
else{
$('#that_select').attr('disabled','disabled');
}
});
document.f1.that_select.value = "b";
<form name="f1" method="post" onload="enable_dropdown(false);">
<input type="checkbox" name="others" onclick="enable_dropdown(this.checked)" >Others
<select id="that_select">
<option value="a">A</option>
<option value="b" selected data-default="true">B</option>
<option value="c">C</option>
</select>
</form>
function enable_dropdown(status)
{
var select = document.f1.that_select;
status = !status;
select.disabled = status;
for (var i = 0; i < select.children.length; ++i) {
if (select.children[i].dataset.default === "true") {
select.value = select.children[i].value;
break;
}
}
}
<form name="f1" method="post">
<input type="checkbox" name="others">Others
<select id="that_select">
<option value="a">A</option>
<option value="b" selected>B</option>
<option value="c">C</option>
</select></form>
$(function(){
$("#that_select").prop('disabled',false);
$("[type='checkbox']").change(function(){
if($(this).is(":checked")) {
$("#that_select").prop('disabled',true);
}
else{
$("#that_select").prop('disabled',false);
}
});
});