douji9518 2012-02-07 18:09
浏览 64

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

My jquery validation script is below. When i click the submit button it does not submit the form data to mysql. The submit button is called submit. If I remove the jquery validation script it submits to mysql so it is an issue with the validation script.

Jquery Validation script:

$(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();
        }
    });
});

submit button:

<input type="submit" name="submit" id="submit" value="Submit" />

form data:

<form name="colour" action="" method="post" id="list">
  • 写回答

1条回答 默认 最新

  • dongzhi6927 2012-02-07 18:19
    关注

    The selector in the submit() is incorrect. You're looking for the form by its id, list. Your selector is looking for tags called <list>.

    if(retVal){$('list').submit();}
    // Should be
    if(retVal){$('#list').submit();}
    
    Update: if it won't submit in IE:
    if (retVal) {
      document.getElementById('list').submit();
    }
    

    Update 2:

    Instead of binding this to the submit button's .click(), bind it to the form's .submit() and return true or 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;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100