test_name retries
test3 3
test2 3
我尝试通过在不同的选项上调用此函数。 我还尝试将整个select语句放在div中,并在div标签
之后点击函数调用function disableField() {
"use strict";
var a = document.forms["myForm"]["donate_type"].value;
if (a == "pet") {
document.getElementById("pet").disabled = false;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = true;
}
if (a == "money") {
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = false;
document.getElementById("service").disabled = true;
}
if (a == "service") {
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = false;
}
}
但我似乎无法禁用所选的字段集。 我有3个不同的字段集,不同的标题宠物,金钱和服务 有人能告诉我我犯了什么错误吗?
提前致谢。
答案 0 :(得分:1)
您错过了一些代码,这些代码使您的代码测试具有挑战性。我根据您的描述和参考文献添加了缺失的元素。
我在select上用onclick
侦听器替换了放置在选项上的onchange
侦听器。
我还修复了您的disable
/ disabled
拼写错误。
function disableField() {
var a = document.forms["myForm"]["donate_type"].value;
if (a === "pet") {
document.getElementById("pet").disabled = false;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = true;
} else if (a === "money") {
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = false;
document.getElementById("service").disabled = true;
} else if (a === "service") {
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = false;
} else {
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = true;
}
}
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = true;

<form name="myForm">
<select name="donate_type" onchange="disableField()">
<option value=""></option>
<option value="pet">Pet</option>
<option value="money">Monetary</option>
<option value="service">Service</option>
</select>
</form>
<fieldset id="pet">
<input type="text" placeholder="pet">
</fieldset>
<fieldset id="money">
<input type="text" placeholder="money">
</fieldset>
<fieldset id="service">
<input type="text" placeholder="service">
</fieldset>
&#13;
答案 1 :(得分:1)
您需要在select
元素上调用该函数。请尝试以下方法:
function disableField() {
var a = document.forms["myForm"]["donate_type"].value;
if (a === "pet") {
document.getElementById("pet").disabled = false;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = true;
}
if (a === "money") {
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = false;
document.getElementById("service").disabled = true;
}
if (a === "service") {
document.getElementById("pet").disabled = true;
document.getElementById("money").disabled = true;
document.getElementById("service").disabled = false;
}
}
&#13;
<form name="myForm">
<input type="text" id="pet" name="" value="pet"/>
<input type="text" id="money" name="" value="money"/>
<input type="text" id="service" name="" value="service"/>
<select name="donate_type" onchange="disableField()">
<option value=""></option>
<option value="pet">Pet</option>
<option value="money">Monetary</option>
<option value="service">Service</option>
</select>
</form>
&#13;