如何在javascript中禁用文本字段和组合框
我在使用另一个组合框禁用textbox和combo box时遇到问题。这是我目前的javascript:如何在javascript中禁用文本字段和组合框,javascript,html,combobox,Javascript,Html,Combobox,我在使用另一个组合框禁用textbox和combo box时遇到问题。这是我目前的javascript: category.onchange = function () { var val = this.options[this.selectedIndex].value; document.getElementById("bos").disabled = (val == "Military","Civilian") ? true : false; document.getEleme
category.onchange = function () {
var val = this.options[this.selectedIndex].value;
document.getElementById("bos").disabled = (val == "Military","Civilian") ? true : false;
document.getElementById("afpsn").disabled = (val == "Dependent","Civilian") ? true : false;
};
这是我的html:
<select name="category" size="1" id="category">
<option selected="selected">Choose...</option>
<option value="Civilian">Civilian</option>
<option value="Military">Military</option>
<option value="Dependent">Dependent</option>
</select>
<select name="bos" id="bos" >
<option value="">Branch of Service</option>
<option value="PAF">PAF</option>
<option value="PA">PA</option>
<option value="PN">PN</option>
<option value="TAS">TAS</option>
</select>
<input id = "afpsn" name="afpsn" type="text" id="afpsn" size="38" placeholder="AFPSN" />
选择
平民
军事的
依靠的
兵种
PAF
帕
PN
助教
当我选择dependent(从属)或Civil(民用)时,其他组合框和文本字段将被禁用,并在我选择military(军用)时启用。请帮忙 试试这个:
您的条件(与字符串的比较
)不正确
编辑:
要在页面加载时附加事件(假设您没有使用jQuery),一种方法是在页面加载时附加事件,如:
function init() {
category.onchange = function () {
var val = this.options[this.selectedIndex].value;
document.getElementById("bos").disabled = (val === "Military" || val === "Civilian") ? true : false;
document.getElementById("afpsn").disabled = (val === "Dependent" || val === "Civilian") ? true : false;
};
}
在你的
...
该示例之所以有效,是因为默认情况下,
jsfiddle.net
会加载js框中的函数。但是在您的情况下,您必须手动执行。我不认为有任何类似于(val==“军用”、“民用”)
请尝试以下代码:
/*Function to check if value is in an array*/
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
/*******************************************/
var s=document.getElementById("elementId");
s.disabled = isInArray(s.value,["Military","Civilian"]); //Function automatically returns "true" or "false" depending on value
我会帮助你的。您也可以编辑逻辑
category.onchange = function () {
var val = this.options[this.selectedIndex].value;
// alert(val);
document.getElementById("bos").disabled = (val === "Military") ? false : true;
document.getElementById("afpsn").disabled = (val === "Military") ? false : true;
document.getElementById("afpsn").disabled = (val === "Dependent" || val === "Civilian" ) ? true : false;
document.getElementById("bos").disabled = (val === "Dependent" || val === "Civilian" ) ? true : false;
};
为什么它在我的页面中不起作用??它没有任何jquery库,是吗?您的代码和此解决方案不需要任何jquery。你可以看到上面的fiddle链接不包含任何jQuery。现在在我的页面上出现问题。。。为什么它不起作用(可能您忘记将事件附加到body onload?如何附加?我该怎么做?
/*Function to check if value is in an array*/
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
/*******************************************/
var s=document.getElementById("elementId");
s.disabled = isInArray(s.value,["Military","Civilian"]); //Function automatically returns "true" or "false" depending on value
category.onchange = function () {
var val = this.options[this.selectedIndex].value;
// alert(val);
document.getElementById("bos").disabled = (val === "Military") ? false : true;
document.getElementById("afpsn").disabled = (val === "Military") ? false : true;
document.getElementById("afpsn").disabled = (val === "Dependent" || val === "Civilian" ) ? true : false;
document.getElementById("bos").disabled = (val === "Dependent" || val === "Civilian" ) ? true : false;
};