任何人都可以幫助發現我的代碼出錯的地方,即使表單字段無效,我也無法阻止表單提交,我一直在這一整天,並嘗試了很多方法,但沒有運氣。如果驗證失敗,防止表單提交
[Display(Name = "Amount to convert")]
[Required(ErrorMessage = " is required")]
[RegularExpression("^[0-9]+$", ErrorMessage = " requires numbers only")]
[Range(1, int.MaxValue, ErrorMessage = " must be more than 1")]
$(function() {
// $("#frmWeightsMeasures").validate();
$("#frmWeightsMeasures").submit(function(event) {
// var isvalidate = $("#frmWeightsMeasures").valid();
// if (isvalidate) {
event.preventDefault();
// }
$.ajax({
type: "POST",
cache: true,
url: form.attr("action"),
data: form.serialize(),
dataType: "json",
error: searchFailed,
success: function(weightsData) {
$("#DisplayConversion").html(weightsData.DisplayConversion);
}
});
});
});
function searchFailed(xhr, errorType, exception) {
var errorMessage = exception || xhr.statusText;
$("#DisplayConversion").html("Sorry, there was a problem with the search.");
}
我已經添加下面的工作代碼,希望這將幫助別人的未來,故事的摩托車,當修改您的代碼,看你刪除的內容。
$(function() {
$("#frmWeightsMeasures").validate();
$("#frmWeightsMeasures").submit(function(event) {
var isvalidate = $("#frmWeightsMeasures").valid();
if (isvalidate) {
event.preventDefault();
var form = $(this);
$.ajax({
type: "POST",
cache: true,
url: form.attr("action"),
data: form.serialize(),
dataType: "json",
error: searchFailed,
success: function(weightsData) {
$("#DisplayConversion").html(weightsData.DisplayConversion);
}
});
}
});
});
function searchFailed(xhr, errorType, exception) {
var errorMessage = exception || xhr.statusText;
$("#DisplayConversion").html("Sorry, there was a problem with the search.");
}
提交如果刪除'[必填]',不它返回正確的錯誤? – andreister
嗨andreister,沒有我然後得到默認mvc錯誤金額轉換是必需的,而不是隻是'是必需的' – CareerChange