【问题标题】:Unable to display Ajax data in table无法在表格中显示 Ajax 数据
【发布时间】:2020-05-05 07:36:26
【问题描述】:

我有一个带有两个下拉菜单、搜索按钮和一个表格的网页,该表格正在使用 jquery 获取有关加载的信息。在加载表格时可以很好地获取信息。但是当我使用下拉菜单和搜索按钮更新此表时,它不起作用,响应控制台中的信息如何。这是我的代码

HTML 代码

<div id="table"></div>

Jquery 加载表

$(document).ready(function(){
    $.ajax({
        url: 'folder/list.php',
        success: function (data) {
            $('#table').html(data);
        },
        error: function(){
            alert('Error: Unable to display');
        }
    });

});

用于搜索的 Jquery

$(document).on('click', '#cont-search', function(event) {
event.preventDefault();
var cate = $('#cat-search').val();
var city = $('#city-search').val(); //Important 

$.ajax({
        url: 'folder/list.php',
        type: 'post',
        dataType: 'json',
        data: {
                cate:cate,
                city:city,
            },
        success: function (data) {
            $('#table').html(data);
        },
        error: function(){
            alert('Error: Unable to display Search');
        }
    });});

PHP 代码

require_once('abc/Item.php');

$cat = $_POST['cate'];
$city = $_POST['city'];

$ser_cont = $item->search_cont($cat,$city);?><div class="table-responsive">
    <table id="myTable-item" class="table table-bordered table-hover table-striped">
        <thead>
            <tr>
                <th>S.No.</th>
                <th>Category</th>
                <th>R No.</th>
                <th>Name</th>
                <th>City</th>
             </tr>
        </thead>
        <tbody>
            <?php 
            $sno = 1;
            foreach($ser_cont as $c): ?>
                <tr align="center">
                    <td><?= $sno++; ?></td>
                    <td><?= $c['cate']; ?></td>
                    <td><?= $c['registno']; ?></td>
                    <td align="left"><?= $c['name']; ?></td>
                    <td><?= $c['dist']; ?></td>
</tr>
            <?php endforeach; ?>
        </tbody>
    </table>

【问题讨论】:

  • 你能把返回的json也添加到你的帖子中吗?
  • 你有没有为你的 $(document).on('click', '#cont-search', function(event) ??? 你需要添加 }) 添加大括号到末尾你的代码...
  • 是的,括号在那里
  • Json 返回与我正在寻找的完全一样。但我不知道为什么它没有显示在表格中,不能将 Json 返回这里
  • 您尝试过什么调试问题?这真的是 PHP 的问题吗?

标签: php html jquery


【解决方案1】:

你可以试试把action方法改成onchange

$(document).on('change', '#cont-search', function(event) {
// your event
}

【讨论】:

【解决方案2】:

我解决了这个问题,因为它不是一个 json 返回,所以我只是删除了

dataType: 'json',

根据我的代码,它成功了,感谢 cmets 的每一个人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多