禁用按钮,直到完成所有必填字段 [英] Disable button until all required fields are completed

查看:109
本文介绍了禁用按钮,直到完成所有必填字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含95个表单字段的表单,其中一些是必填字段,其中一些是可选的。我想禁用提交按钮,直到完成所有必填字段。

I have a form with 95 forms fields, some of which are mandatory and some of which are optional. I want to disable the submit button until all of the required fields are completed.

有很多解决方案,但我正在寻找一个会扫描的输入中需要标志,因为我真的不想在所有95个字段中列出,如果我可以提供帮助的话。

There are a lot of solutions out there but I am looking for one that will scan for the required flag in the input as I really don't want to have to list through all 95 fields if I can help it.

这是否可以用jQuery或其他东西?

Is this possible with jQuery or something?

非常感谢,

John

推荐答案

(function() {
    $('form > input').keyup(function() {

        var empty = false;
        $('form > input[required]').each(function() {
            if ($(this).val() == '') {
                empty = true;
            }
        });

        if (empty) {
            $('#register').attr('disabled', 'disabled');
        } else {
            $('#register').removeAttr('disabled');
        }
    });
})()

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<form>
    Username<br />
    <input type="text" id="user_input" name="username" required /><br />
    Password<br />
    <input type="password" id="pass_input" name="password" required /><br />
    Confirm Password<br />
    <input type="password" id="v_pass_input" name="v_password" required /><br />
    Email<br />
    <input type="text" id="email" name="email" required/><br /> 
  name<br>
  <input type="text" id="name" name="name"/> optional<br />     
    <input type="submit" id="register" value="Register" disabled="disabled" />
  
</form>
<div id="test">
</div>

这篇关于禁用按钮,直到完成所有必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆