私はいくつかの入力がある& selectのWebフォームを持っています。これらのフィールドのデータを送信するAjaxがあります。私はsubmit.pleaseヘルプの前に検証をチェックするために強制的に検証することができます。JQuery:JQueryの検証の前にフォームを送信する理由
function validate_fields() {
var empty_inputs = $('.mandatory').filter(function() {
return !$(this).val();
}).length;
//Checking if any Empty input or select that has class mandatory
if (empty_inputs > 0) {
$('form').find('input.mandatory, select.mandatory').each(function() {
if ($(this).val() == '' || $(this).val() == null) { //Change the color if empty input or select
$(this).css('background-color', '#FFCECE');
} else {
$(this).css('background-color', 'transparent');
};
});
return false;
} else {
return true;
}
}
$('#button_save').click(function() {
$("form").on("submit", function(event) {
var status;
status = validate_fields();
// if fuction validate_fields return false then Stop submit
if (status == false) {
return false;
}
var dt = $(this).serializeArray();
event.preventDefault();
//This Ajax Post the Input data if validation return true.
$.ajax({
url: 'send_data.php',
type: "post",
async: true,
data: dt,
dataType: 'html',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function(data) {
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
をチェックコードは正常に見えます。 – GauravKP