【问题标题】:How to use refresh div correctly?如何正确使用刷新 div?
【发布时间】:2021-11-23 10:12:14
【问题描述】:

嗨,

你能解释一下为什么我的刷新 div 不起作用吗?单击提交时,似乎 div 正在尝试通过删除所有行数据来刷新,但它没有返回任何内容,而是将 div 留空。存储到数据库的数据很好,但我需要 div 刷新并显示所有新提交的数据

$('#submitBtm').on('click', onSubmit = () => {

    const first_nameV = document.getElementById("first_name").value;
    const last_nameV = document.getElementById("last_name").value;
    const emailV = document.getElementById("email").value;
    const departmentV = document.getElementById("department").value;
        
        $.ajax({
            type: "POST",
            url: `companydirectory/libs/php/insertAll.php?first_name=${first_nameV}&last_name=${last_nameV}&email=${emailV}&departmentID=${departmentV}`,
            success: function(data) {
               
            },
            error:  function(request,error) {
                console.log(request)                                   
            }
        })

        $("#id_data").load(location.href + " #id_data");

    event.preventDefault();
})

在 HTML 页面上

<div class="listTable">
     <tbody id="id_data">
                        
     </tbody>
</div>

【问题讨论】:

    标签: javascript html jquery onclick refresh


    【解决方案1】:

    它不会更新,因为您没有附加在成功回调中收到的新数据

      $.ajax({
            type: "POST",
            url: `companydirectory/libs/php/insertAll.php?first_name=${first_nameV}&last_name=${last_nameV}&email=${emailV}&departmentID=${departmentV}`,
            success: function(data) {
               // here you have the data and you can refresh the table
            },
            error:  function(request,error) {
                console.log(request)                                   
            }
        })
    

    【讨论】:

    • 但我没有什么可做的,因为它只是将新数据发布到数据库?我试过把 $("#id_data").load(location.href + " #id_data");里面成功了,还是不行……
    • 成功响应的数据如何?
    • 你是对的!我创建了一个函数,该函数在应用程序首次加载时包裹 getAll() 数据。有了这个,我可以在成功回调中弹出 2,效果很好。谢谢?
    猜你喜欢
    • 2012-02-15
    • 2014-07-14
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    相关资源
    最近更新 更多