【问题标题】:Disable html form submit button until only fields with required attribute entered禁用 html 表单提交按钮,直到仅输入具有必需属性的字段
【发布时间】:2018-04-19 02:09:36
【问题描述】:

我想在输入必填字段之前隐藏 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>

【问题讨论】:

  • 隐藏和禁用是两种不同的场景
  • 如果我想仅在输入具有必填属性的字段时禁用提交按钮怎么办
  • @Hiuyu2001 即使按“Enter”也可以提交表单。没有计划处理该用例?
  • IMO,隐藏提交按钮将是糟糕的 UI/UX 设计。由于您标记了此 HTML5,您可以使用required attribute 来阻止提交,直到所有字段都填写完毕。您可能需要添加一些 JS 作为备用,以防有人使用不支持它的浏览器。

标签: html


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多