簡體   English   中英

我如何停止使用jQuery提交表單

[英]how do i stop a form submit with jQuery

我在這里有這個表格,我不希望他們在沒有選擇的情況下進入下一頁

<form method="post" action="step2/" id="form1">
....
....
....
<input type="submit" class="submit notext" value="Next" />

這是我的jquery

$('.submit').click(function(e) {
    var business = $(".business_type_select").find('.container strong').text();
    alert(business);
    if(business == "Select Business Type"){
        alert("BusinessBusinessBusiness");
        e.preventDefault;
        return false;
    }
});

任何想法我錯過了讓它停止提交

嘗試使用submit事件:

$("#formID").submit(function(e) {
    var business = $(".business_type_select").find('.container strong').text();
    alert(business);
    if(business == "Select Business Type"){
        alert("BusinessBusinessBusiness");
        return false;
    }
});

此外, e.preventDefault()是一個函數,但是冗余,因為return false將起作用。

preventDefault是一個函數 - 使用e.preventDefault()

IE中的.preventDefault()有時會出現問題,嘗試添加:

if (e.preventDefault)      // checks to see if the event has a preventDefault method
    e.preventDefault();
else
    e.returnValue = false;
$("#formID").submit(function(e) {
    var business = $(".business_type_select").find('.container strong').text();
    alert(business);
    if(business == "Select Business Type"){
        alert("BusinessBusinessBusiness");
        e.preventDefault();
        return false;
    }
});

如果你的asp.net MVC剃刀形式看起來像這樣: -

您可以使用(文檔)ID使用JavaScript驗證表單值。 在使用HTML幫助程序(@ValidationFor等)完成驗證之前觸發JavaScript驗證。

@using (Html.BeginForm("MyRequestAction", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true)
code goes here...
}
$(document).submit(function (e) {
    var catVal = $("#Category").val();
    if (catVal == "") {
        alert("Please select Category!");
        return false;
    }
    if (catVal == "--Select One--") {
        alert("Please select Category!");
        return false;
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM