jQuery表单不提交

时间:2013-11-04 12:20:31

标签: jquery jqueryform

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

<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函数。为什么呢?

2 个答案:

答案 0 :(得分: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数据接收的变量名称并相应地命名。

答案 1 :(得分: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/