jQuery阻止from表單的提交


問題:

  在寫Django項目時,用到之前寫的一個登陸界面,里面用JQ給from表單綁定了幾個簡單的校驗事件,然后今天在將表單提交到后台的時候,當校驗不通過時仍然刷新了頁面,造成JQ的提示文本不能正常顯示,於是就想到了怎么去阻止form表單的提交

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="content-Type" charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.css">
    <style>
        body {
            background-color: #eeeeee;
        }
        .login-box {
            margin-top: 50px;
        }

    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4 login-box">
            <form class="form-horizontal" action="" method="post">
                {% csrf_token %}
                <h2 class="text-center">請登錄</h2>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-3 control-label">用戶名</label>
                    <div class="col-sm-9">
                        <input type="text"  name="n1" class="form-control" id="inputEmail3" placeholder="用戶名">
                        <span class="help-block"></span>
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-3 control-label">密碼</label>
                    <div class="col-sm-9">
                        <input type="password" class="form-control"  name="p1" id="inputPassword3" placeholder="密碼">
                        <span class="help-block"></span>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-9">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox"> 記住我
                            </label>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-9">
                        <button type="submit" id="b1" class="btn btn-block btn-primary" >登錄</button>
                        <p style="text-align: center;color: red">{{ error_msg }}</p>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="/static/jQuery.js"></script>
<script>
    $("#b1").click(function () {
        $("input:not([type='checkbox'])").each(function () {
            // 判斷值不為空
            if ($(this).val().length === 0){
                // 展示錯誤提示
                var errMsgPrefix = $(this).parent().prev().text();
                $(this).next().text(errMsgPrefix + "不能為空");
                $(this).parent().parent().addClass("has-error");
                
            }
        });
    });
    
    // 給輸入框綁定獲取焦點的事件
    $("input:not([type='checkbox'])").focus(function () {
        // 清空錯誤提示
        $(this).next().text("");
        // 移除父標簽的has-error
        $(this).parent().parent().removeClass("has-error");
    });
</script>
</body>
</html>
原代碼

 

  然后在網上搜到了jQuery 事件 - preventDefault() 方法

  定義和用法:preventDefault() 方法阻止元素發生默認的行為(例如,當點擊提交按鈕時阻止對表單的提交)。

  實例:防止鏈接打開 URL:

$("a").click(function(event){
  event.preventDefault();
});

 對照將原代碼中JQ語句改成如下

 $("#b1").click(function (event) {
        $("input:not([type='checkbox'])").each(function () {
            // 判斷值不為空
            if ($(this).val().length === 0){
                // 展示錯誤提示
                var errMsgPrefix = $(this).parent().prev().text();
                $(this).next().text(errMsgPrefix + "不能為空");
                $(this).parent().parent().addClass("has-error");
                event.preventDefault();
            }
        });
    });

 

再此執行,當不輸入內容時,頁面不跳轉並顯示輔助文本,當校驗通過時正常跳轉

另附:w3school  關於此方法的解釋


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM