我在提交以下表单时遇到问题。
对于背景,我正在尝试“提交”交付表格,我需要知道a)他们的取件地址,b)他们的下车地址,以及c)他们的描述。如果<p class="error">
为空,我创建了<input>
个字段(如“请输入说明”)。
如果我删除'return false;'无论如何,表格都会提交,但如果我保留“返回虚假”; jQuery工作(即 - 出现错误信息),但现在表单永远不会提交。想法?
这是我的main.js
var main = function() {
$('form').submit(function() {
var pickup = $('#pickup').val();
if(pickup === "") {
$('.pickup-error').text("Please choose a pickup.");
}
var dropoff = $('#dropoff').val();
if(dropoff === "") {
$('.dropoff-error').text("Please choose a dropoff.");
}
var description = $('#description').val();
if(description === "") {
$('.description-error').text("Please tell us a little about what we're moving.");
}
return false;
});
};
$(document).ready(main);
答案 0 :(得分:1)
var main = function () {
$('form').submit(function () {
var pickup = $('#pickup').val();
if (pickup === "") {
$('.pickup-error').text("Please choose a pickup.");
}
var dropoff = $('#dropoff').val();
if (dropoff === "") {
$('.dropoff-error').text("Please choose a dropoff.");
}
var description = $('#description').val();
if (description === "") {
$('.description-error').text("Please tell us a little about what we're moving.");
}
// did not pass validation
if (pickup != "" || dropoff != "" || description != "") {
return false;
}
// passed validation, submit
return true;
});
};
$(document).ready(main);