任何人都可以帮助找出我的代码出错的地方吗?即使表单字段无效,我也无法阻止表单提交,我整天都在这样做并且尝试了很多方法但没有运气。
[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.");
}