【问题标题】:Enable and Disable input field using Javascript使用 Javascript 启用和禁用输入字段
【发布时间】:2020-12-08 21:28:25
【问题描述】:

我编写了一些简单的代码,可以在单击按钮时启用和禁用输入字段,但它没有响应。 请帮帮我……!!!

<html>
<head>
    <script type="text/javascript">
        function toggleEnable(el1, el2) {
            document.getElementByID(el1).disabled = true;
            document.getElementByID(el2).disabled = true;
        }
    </script>
</head>
<body>
    <form>
        <table>
            <tr>
                <td><input id="input1" class="myText" type="text" placeholder="Row 1" /></td>
                <td><input id="input2" class="myText" type="text" placeholder="Row 1" /></td>
                <td><button onclick="toggleEnable('input1','input2')"> Enable/Disable </button></td>
            </tr>
            <tr>
                <td><input id="input3" class="myText" type="text" placeholder="Row 3" /></td>
                <td><input id="input4" class="myText" type="text" placeholder="Row 4" /></td>
                <td><button onclick="toggleEnable('input3','input4')"> Enable/Disable </button></td>
            </tr>
        </table>
    </form>
</body>
</html>

【问题讨论】:

标签: javascript css forms


【解决方案1】:

您的按钮正在提交表单,要不提交表单,您必须使用type="button" 按钮,而且您将getElementByID 拼写错误为getElementById

<form>
    <table>
        <tr>
            <td><input id="input1" class="myText" type="text" placeholder="Row 1" /></td>
            <td><input id="input2" class="myText" type="text" placeholder="Row 1" /></td>
            <td><button type="button" onclick="toggleEnable('input1','input2')"> Enable/Disable </button></td>
        </tr>
        <tr>
            <td><input id="input3" class="myText" type="text" placeholder="Row 3" /></td>
            <td><input id="input4" class="myText" type="text" placeholder="Row 4" /></td>
            <td><button type="button" onclick="toggleEnable('input3','input4')"> Enable/Disable </button></td>
        </tr>
    </table>
</form>
    function toggleEnable(el1, el2) {
        document.getElementById(el1).disabled = true;
        document.getElementById(el2).disabled = true;
    }

http://jsfiddle.net/mowglisanu/aam7jg9t/

【讨论】:

    【解决方案2】:

    function toggleEnable(id) {
       var textbox = document.getElementById(id);
       
       if (textbox.disabled) {
          // If disabled, do this 
           document.getElementById(id).disabled = false;
        } else {
           // Enter code here
            document.getElementById(id).disabled = true;
         }
    }
    <form>
        <table>
            <tr>
                <td><input id="input1" class="myText" type="text" placeholder="Row 1" /></td>
                <td><button type="button" onclick="toggleEnable('input1')"> Enable/Disable </button></td>
            </tr>
            <tr>
                <td><input id="input2" class="myText" type="text" placeholder="Row 2" /></td>
                <td><button type="button" onclick="toggleEnable('input2')"> Enable/Disable </button></td>
            </tr>
        </table>
    </form>

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 2015-06-24
      • 2017-05-19
      相关资源
      最近更新 更多