使用JavaScript禁用字段集

时间:2017-03-21 01:46:05

标签: javascript jquery html

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个不同的字段集,不同的标题宠物,金钱和服务 有人能告诉我我犯了什么错误吗?

提前致谢。

2 个答案:

答案 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;
&#13;
&#13;

答案 1 :(得分:1)

您需要在select元素上调用该函数。请尝试以下方法:

&#13;
&#13;
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;
&#13;
&#13;