Jquery:无法获取。请使用默认设置
单击submit按钮,我试图捕获submit并快速执行AJAX请求,以查看是否已经存在表单中指定的时间和日期的预订。如果是这样,请停止表单提交并提醒用户已存在日期和时间预订!如果没有预订,请继续并提交表格。就我个人而言,我无法让Jquery:无法获取。请使用默认设置,jquery,preventdefault,Jquery,Preventdefault,单击submit按钮,我试图捕获submit并快速执行AJAX请求,以查看是否已经存在表单中指定的时间和日期的预订。如果是这样,请停止表单提交并提醒用户已存在日期和时间预订!如果没有预订,请继续并提交表格。就我个人而言,我无法让.preventDefault正常工作,除非我将它放在提交函数的末尾。非常感谢任何想法和建议,我在这方面已经坚持了三个小时,似乎进展不快。我99%肯定我只是个白痴,所以提前道歉吧 这是我的密码: $('#formID').submit(function(event){
.preventDefault
正常工作,除非我将它放在提交函数的末尾。非常感谢任何想法和建议,我在这方面已经坚持了三个小时,似乎进展不快。我99%肯定我只是个白痴,所以提前道歉吧
这是我的密码:
$('#formID').submit(function(event){
var InspectionDate = $('#datepicker').val().split('/');
InspectionDate.reverse();
InspectionDate = InspectionDate.join('-');
InspectionHour = $('#time_hour').val();
InspectionMinutes = $('#time_minutes').val();
var InspectionDateTime = InspectionDate + ' ' + InspectionHour + ':' + InspectionMinutes + ':00';
$.ajax({
type: "POST",
url: "ajax_booking_check.php",
data: 'InspectionDateTime='+ InspectionDateTime,
cache: false,
success: function(response){
if(response = 1){
alert("An appointment for the selected Date and Time already exists.\n\nDouble Bookings are not possible.\n\nPlease refer to the calender to find an available appointment.");
event.preventDefault();
}
else{
//submit form
}
}
});
});
您需要将
事件.preventDefault
放在方法的开头,而不是成功回调
$('#formID').submit(function(event){
event.preventDefault();
var InspectionDate = $('#datepicker').val().split('/');
...
});
将preventDefault作为第一行,然后如果希望表单提交,请调用表单元素上的submit方法。通过调用表单元素的submit方法而不是jQuery定义的方法,它将绕过绑定jQuery的提交事件处理程序
$('#formID').submit(function (event) {
event.preventDefault();
var form = this;
var InspectionDate = $('#datepicker').val().split('/');
InspectionDate.reverse();
InspectionDate = InspectionDate.join('-');
InspectionHour = $('#time_hour').val();
InspectionMinutes = $('#time_minutes').val();
var InspectionDateTime = InspectionDate + ' ' + InspectionHour + ':' + InspectionMinutes + ':00';
$.ajax({
type: "POST",
url: "ajax_booking_check.php",
data: 'InspectionDateTime=' + InspectionDateTime,
cache: false,
success: function (response) {
if (response = 1) {
alert("An appointment for the selected Date and Time already exists.\n\nDouble Bookings are not possible.\n\nPlease refer to the calender to find an available appointment.");
} else {
form.submit();
}
}
});
});
您是如何使用preventDefault的?抱歉,不确定您所说的我是如何使用preventDefault的?抱歉。。忘记删除此评论。。在成功回访中看到了谢谢Kevin。。。我知道这会很简单!非常感谢。干杯!现在明白了,我知道这很简单。