ajax表单在提交数据时忽略我的复选框重置为默认值

时间:2014-06-03 06:45:02

标签: javascript jquery html ajax

我的网站有一个HTML表单,需要通过JQuery AJAX提交:

我的问题是,提交数据时会忽略检查按钮值。

HTML code:

<form id="form" name="form">
        <fieldset id="fieldset">
            <legend id="legend">Filter</legend>
            <input type="checkbox" name="filterFailed" id="filterFailed" a>Failed
            <input type="checkbox" name="filterAlert" id="filterAlert" >Alert
            <input type="checkbox" name="filterWarning" id="filterWarning" >Warning
            <input type="checkbox" name="filterNotReceived" id="filterNotReceived" >Not Received
            <input type="checkbox" name="filterOther" id="filterOther" >Other
            <input type="checkbox" name="filterSuccess" id="filterSuccess" >Success
            <input type="submit"/>
        </fieldset>
    </form>

Javascript代码:

$(document).ready(function() {
        $('form').submit(function(event) {
            e.preventDefault();
                getNotifications();
        });
    });

    document.form["filterFailed"].checked = true,
    document.form["filterAlert"].checked,
    document.form["filterWarning"].checked,
    document.form["filterNotReceived"].checked,
    document.form["filterOther"].checked,
    document.form["filterSuccess"].checked


    getNotifications();

    function getNotifications() {
        var formData = {
            'getAllNotifications': 1,
            'filterFailed'       : document.form["filterFailed"].checked,
            'filterAlert'        : document.form["filterAlert"].checked,
            'filterWarning'      : document.form["filterWarning"].checked,
            'filterNotReceived'  : document.form["filterNotReceived"].checked,
            'filterOther'        : document.form["filterOther"].checked,
            'filterSuccess'      : document.form["filterSuccess"].checked
        };

        $.ajax({
            url     : 'server_script.php',
            type    : 'post',
            data    : formData,
            success :function(response) {
                    alert(response);

            },
        });
    }

问题:

为什么在提交表单时复选框会重置为默认值(我的PHP脚本始终收到默认复选框状态“filterFailed = true”忽略我的选择)?

1 个答案:

答案 0 :(得分:0)

Mohit Arora的解决方案:

$(document).ready(function() {
    $('form').submit(function(event) {
        event.preventDefault();
            getNotifications();
    });
});