最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how do i stop a form submit with jQuery - Stack Overflow

programmeradmin0浏览0评论

I have this form here and i dont want them to go to the next page without certain selections

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

and here is my 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;
    }
});

any ideas what i am missing to get this to stop submitting

I have this form here and i dont want them to go to the next page without certain selections

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

and here is my 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;
    }
});

any ideas what i am missing to get this to stop submitting

Share Improve this question asked May 10, 2011 at 20:48 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 8

Try using the submit event:

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

Also, the e.preventDefault() is a function, but is redundant as the return false will work just the same.

preventDefault is a function - use e.preventDefault().

There are sometimes issues with .preventDefault() in IE try adding this:

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

If your asp MVC razor form looks something like this:-

You can use (document) ID to validate form values using JavaScript. JavaScript validations fire prior to validations done using HTML Helpers ( @ValidationFor etc)..

@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;
    }
});
发布评论

评论列表(0)

  1. 暂无评论