提交按钮未将表单数据发送到 mysql - jquery 问题


Submit button not sending form data to mysql - jquery issue

我的jquery验证脚本如下。当我单击提交按钮时,它不会将表单数据提交到 mysql。提交按钮称为提交。如果我删除 jquery 验证脚本,它会提交给 mysql,因此验证脚本存在问题。

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:

不要将其绑定到提交按钮的.click(),而是将其绑定到表单的.submit()并返回 true 或 false:

// 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;
});