简体   繁体   中英

Disabled submit button not enabling with JQuery

I have added this JQuery - found on SO to validate my form so that fields must be filled before the submit button can be clicked, but the button never un-disables.

Form

<form action="homepage.php" method="POST" id="form">

<div class="medium-50 columns">
<label>Phone Number</label>
<input type="text" placeholder="Enter Your Phone Number" name="phone" />
</div>

<input type="submit" id="register" value="Submit" name="submit" disabled="disabled">

</form>

JQuery

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

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

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

What is wrong?

It doesn't work because 'input' isn't a child of 'form'.

Take a look here and here .

Try this:

 (function() { $('form input').keyup(function() { var empty = false; $('form input').each(function() { if ($(this).val() == '') { empty = true; } }); console.log(empty); if (empty) { $('#register').attr('disabled', 'disabled'); } else { $('#register').removeAttr('disabled'); } }); })() 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="homepage.php" method="POST" id="form"> <div class="medium-50 columns"> <label>Phone Number</label> <input type="text" placeholder="Enter Your Phone Number" name="phone" /> </div> <input type="submit" id="register" value="Submit" name="submit" disabled="disabled"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM