如果验证失败,我会尝试阻止表单提交。我试着遵循这个previous post,但我不为我自己工作。我遗漏了什么?
<input id="saveButton" type="submit" value="Save" />
<input id="cancelButton" type="button" value="Cancel" />
<script src="../../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("form").submit(function (e) {
$.ajax({
url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
data: { id: '@Model.ClientId' },
success: function (data) {
showMsg(data, e);
},
cache: false
});
});
});
$("#cancelButton").click(function () {
window.location = '@Url.Action("list", "default", new { clientId = Model.ClientId })';
});
$("[type=text]").focus(function () {
$(this).select();
});
function showMsg(hasCurrentJob, sender) {
if (hasCurrentJob == "True") {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
if (sender != null) {
sender.preventDefault();
}
return false;
}
}
</script>
发布于 2014-08-16 02:07:36
一种不同的方法可以是
<script type="text/javascript">
function CheckData() {
//you may want to check something here and based on that wanna return true and false from the function.
if(MyStuffIsokay)
return true;//will cause form to postback to server.
else
return false;//will cause form Not to postback to server
}
</script>
@using (Html.BeginForm("SaveEmployee", "Employees", FormMethod.Post, new { id = "EmployeeDetailsForm" }))
{
.........
.........
.........
.........
<input type="submit" value= "Save Employee" onclick="return CheckData();"/>
}
发布于 2018-07-05 18:29:52
这是用于防止提交的JQuery代码
$('form').submit(function (e) {
if (radioButtonValue !== "0") {
e.preventDefault();
}
});
https://stackoverflow.com/questions/10092580
复制相似问题