在同一表格PHP jQuery上POST后无法显示表格

时间:2014-04-02 12:20:32

标签: php jquery

我在下面的代码中获取了来自POST的数据,并将数据作为表格返回。代码运行后,我按Console Log按钮,查看按下SUBMIT按钮后发生的情况。控制台日志显示为"完成"我想要显示表格,但不知道为什么它没有发生。

<script type="text/javascript">
$(function(){
$('#searchform').on('submit', function(e){
    e.preventDefault();
        //alert($('#searchpostcode').val())
        $.post('includes/jobdetailssearch.php', 
        $('#searchform').serialize(), 
        function(data, status){
            $('.table-responsive #displayadd').html(data.Display);
            //$("#table-responsive td").last().append(data);
        console.log("done");
    }).fail(function () {
        console.log("fail");
    });
});
});
</script>

为什么会这样?

2 个答案:

答案 0 :(得分:0)

确保您的php脚本返回一个序列化的json结构,可以通过javascript解析。

尝试在回调函数中console.log(data)查看从PHP返回的内容。

答案 1 :(得分:0)

试试这个:

<script type="text/javascript">
$(function(){
    // Submit Button Pressed
    $(document).on('submit','#searchform', function(e){
        // Prevent Default Click
        e.preventDefault();
        //The first parameter of $.post() is the URL we wish to request
        $.post("includes/jobdetailssearch.php",
        {
            //Then we pass in some data to send along with the request
            searchval:$('#searchform').val()
        },
        //The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
        function(data,status){
         alert("Data: " + data + "\nStatus: " + status);
        });

    });

});
</script>

注意:

$('#searchform').on('submit'$('#searchform').submit(的工作方式相同,但是当您使用$(document).on('submit','#searchform'时,它也适用于稍后添加的DOM。