jQuery unbind无法正常工作

时间:2017-05-29 11:38:13

标签: jquery

在我的html表单中,当我将输入放在一个或两个字段中时,我使用jquery验证输入字段,但是当我输入整个字段时它没有提交值。

以下javascript代码

<script type="text/javascript">
    $("form").submit(function(e) {
        e.preventDefault();
        var error = "";
        if ($("#email").val() == "") {
            error += "The email field are required!!<br>";
        }
        if ($("#subject").val() == "") {
            error += "The subject field are required!!<br>";
        }
        if ($("#content").val() == "") {
            error += "The massage field are required!!";
        }
        if (error != "") {
            $("#error").html('<div class="alert alert-danger" role="alert"><strong>There are some missing field:</strong><br>' + error + '</div>');
        } else {
            $("form").unbind("submit").submit();
        }
    });
</script> 

下面的代码

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $("form").submit(function(e) {
            e.preventDefault();
            var error = "";
            if ($("#email").val() == "") {
                error += "The email field are required!!<br>";
            }
            if ($("#subject").val() == "") {
                error += "The subject field are required!!<br>";
            }
            if ($("#content").val() == "") {
                error += "The massage field are required!!";
            }
            if (error != "") {
                $("#error").html('<div class="alert alert-danger" role="alert"><strong>There are some missing field:</strong><br>' + error + '</div>');
            } else {
                $("form").unbind("submit").submit();
            }
        });
 </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

<div class="container">
        <h2>Get in Touch!</h2>
        <div id="error"></div>
        <form>
            <div class="form-group">
                <label for="email">Email address</label>
                <input type="email" class="form-control" id="email" placeholder="Enter your email address">
                <p class="help-block">We will never share your email with anyone.</p>
            </div>
            <div class="form-group">
                <label for="subject">Subject</label>
                <input type="text" class="form-control" id="subject">
            </div>
            <div class="form-group">
                <label for="content">Tell brief about your project.</label>
                <textarea class="form-control" id="content" rows="3"></textarea>

            </div>

            <button type="submit" id="submit" class="btn btn-primary">Submit</button>
        </form>

    </div>

1 个答案:

答案 0 :(得分:0)

使用.off()即。$(“form”)。off(“submit”)。submit();

我将unbind替换为off for working for me :)