javascript - 如何根据复选框状态禁用/启用提交按钮?

标签 javascript jquery html validation checkbox

有一个表单,其中有两个文本字段和一个复选框,所有这些都是必需的,并且具有 required 属性。

只有在填写并检查了所需的输入后,才应启用提交按钮。

使用当前代码,文本字段验证似乎工作正常,但它对复选框没有影响。

Jsfiddle

<form action="#otherForm">
   Name * <input name="otherForm-name1" placeholder="required" required> <br />
   Tel * <input name="otherForm-surname" placeholder="required" required> <br />
   <input type="checkbox" name="otherForm-chcekbox" required><label for="otherForm-chcekbox">I agree</label> <br />
   <button id="otherForm-submitBtn" class="monitored-btn" type="submit">Submit</button>
</form>

<script>
    const inputSelector = ':input[required]:visible';

    function checkForm() {
        // here, "this" is an input element
        var isValidForm = true;
        $(this.form).find(inputSelector).each(function() {
            if (!this.value.trim()) {
                isValidForm = false;
            }
        });
        $(this.form).find('.monitored-btn').prop('disabled', !isValidForm);
        return isValidForm;
    }
    $('.monitored-btn').closest('form')
        // in a user hacked to remove "disabled" attribute, also monitor the submit event
        .submit(function() {
            // launch checkForm for the first encountered input,
            // use its return value to prevent default if form is not valid
            return checkForm.apply($(this).find(':input')[0]);
        })
        .find(inputSelector).keyup(checkForm).keyup();
</script>

最佳答案

我的做法与 Kubwimana Adrien 的答案非常相似,最初渲染一个禁用的按钮。

此外,一般来说,将验证 bool 值的初始状态设置为 TRUE 并不被认为是安全的。因此,稍微改变一下代码。

    const inputSelector = ':input[required]:visible';

    function checkForm() {
        // here, "this" is an input element
        var isValidForm = false;
        $(this.form).find(inputSelector).each(function() {
            if (((this.type != 'checkbox') && (this.value.trim() != "")) || (this.type == 'checkbox' && this.checked)) {
                isValidForm = true;
            }
            else{
            	isValidForm = false
                return false //break out of each loop
            }
        });
        $(this.form).find('.monitored-btn').prop('disabled', !isValidForm);
        return isValidForm;
    }
    $('.monitored-btn').closest('form')
        // in a user hacked to remove "disabled" attribute, also monitor the submit event
        .submit(function() {
            // launch checkForm for the first encountered input,
            // use its return value to prevent default if form is not valid
            return checkForm.apply($(this).find(':input')[0]);
        })
        .find(inputSelector).on("keyup change", checkForm);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <form action="#otherForm">
         Name * <input name="otherForm-name1" placeholder="required" required> <br />
         Tel * <input name="otherForm-surname" placeholder="required" required> <br />
         <input type="checkbox" name="otherForm-chcekbox" required><label for="otherForm-chcekbox">I agree</label> <br />
         <button id="otherForm-submitBtn" class="monitored-btn" type="submit" disabled>Submit</button>
  </form></body>

关于javascript - 如何根据复选框状态禁用/启用提交按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54083739/

相关文章:

jquery - 修改 InfiniteScroll 使其水平工作

html - Easel.js > 这段代码有什么问题?

具有 float div 和合并列的 CSS3 列数

javascript - 如何找到内部元素的宽度

javascript - DJANGO 使用 ajax 在基本模板中加载/包含模板

c# - 如何使用 jquery 从第一个列表框中选择来填充第二个列表框?

jQuery - 检查刷新时的选择值?

jquery - 通过 Ajax 发送数据并在操作 Controller 上处理它们

javascript - 模拟网页中的按钮单击

javascript - 将 JavaScript 值放入变量中