0

我无法提交表格。我哪里错了?

<div>
    <form id = "search_form" action = "/dashboard/" method = "post">
        <span class = "pull-right">
        Search : <input type = "text" placeholder = "Search" id = "search">
        </span>
    </form>
</div>

jQuery

$("#search_form").submit(function(event) {
    event.preventDefault();
    var formdata = $(this).serialize();
    alert(formdata);
    $.ajax({
        type: "POST",
        url: "gallery.php",
        data: formdata,
        success: function(){alert('success');}
    });
});

提交后,流程不会进入 jQuery 函数。为什么?

4

2 回答 2

2

您没有为Search字段命名,因此您没有向gallery.php文件传递任何值,这可能会导致服务器端错误,因为它需要一个值。

<div>
    <form id="search_form" action="/dashboard/" method="post">
        <span class="pull-right">
        Search : <input type="text" placeholder="Search" id="search" name="search">
        </span>
    </form>
</div>

请注意,我已将字段命名为search,您需要检查 PHP 文件从 POST 数据接收的变量名称并相应地命名。

于 2013-11-04T12:22:24.670 回答
1

尝试:

$(function() {
    $("#search_form").submit(function(event) {
        event.preventDefault();
        var formdata = $(this).serialize();
        alert(formdata);
        $.ajax({
            type: "POST",
            url: "gallery.php",
            data: formdata,
            success: function(){alert('success');}
        });
    });
});

一切正常:http: //jsfiddle.net/J6e4G/

于 2013-11-04T12:29:47.787 回答