Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery停止表单提交_Javascript_Jquery - Fatal编程技术网

Javascript 如何使用jQuery停止表单提交

Javascript 如何使用jQuery停止表单提交,javascript,jquery,Javascript,Jquery,我有这个表格,我不想让他们在没有特定选择的情况下进入下一页 <form method="post" action="step2/" id="form1"> .... .... .... <input type="submit" class="submit notext" value="Next" /> 如果我想让它停止提交,我缺少的是一个函数-使用e.preventDefault()尝试使用submit事件: $("#formID").submit(function(e

我有这个表格,我不想让他们在没有特定选择的情况下进入下一页

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

如果我想让它停止提交,我缺少的是一个函数-使用
e.preventDefault()
尝试使用
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()
是一个函数,但它是多余的,因为
返回false
的工作原理相同。

IE中的
.preventDefault()
有时会出现问题,请尝试添加以下内容:

$("#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 (e.preventDefault)      // checks to see if the event has a preventDefault method
    e.preventDefault();
else
    e.returnValue = false;

如果您的asp.net MVC razor表单如下所示:-

您可以使用(文档)ID使用JavaScript验证表单值。JavaScript验证在使用HTML帮助程序(@ValidationFor etc)完成验证之前触发


e.preventDefault
应该是一个函数call@tamer:尝试将我的答案与@Jesse的答案组合起来。。使用
e.preventDefault()
绑定到
.submit
事件。
if (e.preventDefault)      // checks to see if the event has a preventDefault method
    e.preventDefault();
else
    e.returnValue = false;
@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;
    }
});