我选择婚姻状态后尝试禁用该表,我只想根据特定条件启用名称字段和婚姻状况选择。但似乎没有用。或许有其他方式来编码吗?
<form name="mainform" method="post" action="abc.jsp" >
<table id="style1">
<tr>
<td>name
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<td>age
<input type="text" name="age" id="age">
</td>
</tr>
<tr>
<td>tel no.
<input type="text" name="telno" id="telno">
</td>
</tr>
<tr>
<td>email
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<td>marital status
<select name="ms" id="ms" onChange="fnTest();">
<option>married</option>
<option>single</option>
</select>
</td>
</tr>
</table>
function fnTest() {
document.mainform.getElementById("style1").disabled = true;
//CONDITION
document.mainform.getElementById("name").disabled = false; }
答案 0 :(得分:3)
尝试在输入中添加类,如下所示:
<input type="text" class='information' name="name" id="name">
然后试试这段代码:
function toggle(bool){
if(bool == 'true') {
document.getElementsByClassName('information').disabled = true;
} else {
document.getElementsByClassName('information').disabled = false;
}
}
如何调用该函数:
toggle('true');
答案 1 :(得分:1)
document.mainForm.getElementById
尝试document.getElementById
。<fieldset>
中包含您的输入并禁用它而不是<table>
<fieldset>
中包含要禁用的输入组,并将具有单独规则的控件放在此组外禁用。以下是demo:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="mainform" method="post" action="abc.jsp">
<table>
<tr>
<td>name
<input type="text" name="name" id="name">
</td>
</tr>
</table>
<fieldset id="myform">
<table id="style1">
<tr>
<td>age
<input type="text" name="age" id="age">
</td>
</tr>
<tr>
<td>tel no.
<input type="text" name="telno" id="telno">
</td>
</tr>
<tr>
<td>email
<input type="text" name="email" id="email">
</td>
</tr>
<tr>
<td>marital status
<select name="ms" id="ms" onChange="fnTest();">
<option>married</option>
<option>single</option>
</select>
</td>
</tr>
</table>
</fieldset>
</form>
<script>
function fnTest() {
document.getElementById("myform").disabled = true;
//Reverse your condition to disable name input as well
//document.getElementById("name").disabled = true;
}
</script>
</body>
</html>
答案 2 :(得分:0)
我用你的代码制作了一个fiddle然后调整了一下。这是你的目标吗?
HTML:
<table id="style1">
<tr>
<td>name
<input class='style2' type="text" name="name" id="name"/>
</td>
</tr>
<tr>
<td>age
<input class='style2' type="text" name="age" id="age"/>
</td>
</tr>
<tr>
<td>tel no.
<input class='style2' type="text" name="telno" id="telno"/>
</td>
</tr>
<tr>
<td>email
<input class='style2' type="text" name="email" id="email"/>
</td>
</tr>
<tr>
<td>marital status
<select class='style2' name="ms" id="ms">
<option>Select One</option>
<option>married</option>
<option>single</option>
</select>
</td>
</tr>
</table>
JS:
var fnTest = function() {
alert('change');
var list = document.getElementsByClassName("style2");
for (var l=0;l<list.length;l++) {
list[l].disabled = true;
}
if (true) { //Your Condition
document.getElementById("name").disabled = false;
document.getElementById("ms").disabled = false;
} else {
document.getElementById("name").disabled = true;
document.mainform.getElementById("ms").disabled = true;
}
};
document.getElementById('ms').addEventListener('change', function() { fnTest(); });