禁用html表单提交按钮,直到只输入具有必需属性的字段

时间:2018-04-19 02:09:36

标签: html5

我想隐藏html表单的提交按钮,直到输入所需的字段,有人可以告诉我该怎么做吗? 这是我的代码:

<form name="signup" action="mailto:chowhiuyu@gmail.com" method="post" enctype="text/plain">
    Enter Student ID:
    <input type="text" name="student_ID" size="30%" required >
    <br>Gender: <input type="radio" name="gender" value="male" required>Male <input type="radio" name="gender" value="Female" required>Female
    <br>Form:<select name="Form" required>
                <option value="F1">F1</option>
                <option Value="F2">F2</option>
                <option value="F3">F3</option>
                <option value="F4">F4</option>
                <option value="F5">F5</option>
                <option value="F6">F6</option>
            </select>
    <br>Electives chosen:
        <input type="checkbox" name="Elective" value="Physics">Physics
        <input type="checkbox" name="Elective" value="Chemistry">Chemistry
        <input type="checkbox" name="Elective" value="Biology">Biology
        <br>
        <input type="checkbox" name="Elective" value="History">History
        <input type="checkbox" name="Elective" value="Chinese History">Chinese History
        <input type="checkbox" name="Elective" value="Music">Music
        <input type="checkbox" name="Elective" value="Englit">English Literature
        <br>
        <input type="checkbox" name="Elective" value="Physical Education">Physical Education
        <input type="checkbox" name="Elective" value="Visual Arts">Visual Arts
        <input type="checkbox" name="Elective" value="business">Business Management
        <input type="checkbox" name="Elective" value="Accounting">Accounting
    <br>
        Password:
    <input type="password" name="password" placeholder="Password" id="password" size="30%" required>
    <br>Re-enter Password: 
    <input type="password" name="password_check" placeholder="Confirm Password" id="confirm_password" size="30%" required>
    <p>
        <input type="submit" name="submit" value="Submit" onclick="window.alert('Your Request will be processed soon!');">
        <input type="reset" name="Reset" Value="Reset">
    </p>

1 个答案:

答案 0 :(得分:0)

查看此http://jsfiddle.net/qKG5F/641/

(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');
        }
    });
})()

如果至少有一个字段为空,它将保持按钮被禁用。