javascript - 使用 Javascript 启用和禁用输入字段

标签 javascript css forms

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

<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>

最佳答案

您的按钮正在提交它们所在的表单,要不提交您必须使用 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/

关于javascript - 使用 Javascript 启用和禁用输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677575/

相关文章:

javascript - 基本表单验证函数 checkRadio() 未发布错误消息

Javascript FinishMessage 超链接

html - 导航栏不会在 Bootstrap 3 中居中

javascript - 为什么javascript中的nextSibling方法不能正常工作

javascript - 更改 Wordpress 网站下拉框中的元素

html - 我的网站的响应问题

javascript - 我的 HTML 表单不会返回数组中的值?

javascript - Node.js 对象实例化是否为每个用户创建一个对象?

javascript - Highcharts 散点图中的亮点?

javascript - jQuery UI 可折叠面板