我的 jquery 验证脚本如下。当我单击提交按钮时,它不会将表单数据提交到 mysql。提交按钮称为提交。如果我删除它提交给 mysql 的 jquery 验证脚本,那么这是验证脚本的问题。
Jquery 验证脚本:
$(function() {
function validate(id) {
var enabled = ($("input[name='attendance" + id + "']:checked").val() == 'Yes');
if (enabled) {
//Please select option is selected
if ($("#food" + id)[0].selectedIndex == 0 || $("#drink" + id)[0].selectedIndex == 0) {
alert('Please select color');
return false;
}
}
return true;
};
$("input[name^='attendance']").click(function(e) {
var id = this.name.replace('attendance', '');
$("#food" + id + ", #drink" + id).prop("disabled", this.value == 'No');
validate(id);
});
$("input:submit").click(function(event) {
event.preventDefault();
var retVal = false;
$.each([1, 2], function(i, val) {
retVal = (validate(val) || retVal);
});
if (retVal) {
$('list').submit();
}
});
});
提交按钮:
<input type="submit" name="submit" id="submit" value="Submit" />
表单数据:
<form name="colour" action="" method="post" id="list">
最佳答案
submit()
中的选择器是不正确的。您正在通过其 ID list
查找表单.您的选择器正在寻找名为 <list>
的标签.
if(retVal){$('list').submit();}
// Should be
if(retVal){$('#list').submit();}
更新:如果它不会在 IE 中提交:
if (retVal) {
document.getElementById('list').submit();
}
更新 2:
而不是将其绑定(bind)到提交按钮的 .click()
, 将其绑定(bind)到表单的 .submit()
并返回真或假:
// Bind to the form's .submit()
$("#list").submit(function(event) {
event.preventDefault();
var retVal = false;
$.each([1, 2], function(i, val) {
retVal = (validate(val) || retVal);
});
// return the true/false to the submit action
// false will prevent submission.
return retVal;
});
关于php - 提交按钮不发送表单数据到 mysql - jquery 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9181463/