简体   繁体   English

如何使用jQuery刷新页面或div以显示新数据

[英]How to refresh a page or div with jquery in order to reveal new data

I've got some jquery that submits a form for me. 我有一些jquery可以为我提交表单。 It works just fine. 它工作正常。 The only major problem is how simple it is. 唯一的主要问题是它有多简单。

The form submits the info to the database using my php script without refreshing the page, but the page isn't updated in any way to show the new data. 表单使用我的php脚本将信息提交到数据库,而无需刷新页面,但是页面没有以任何方式更新以显示新数据。 What are some good ways to update the page or div so my new data is displayed after submitting the form. 有什么好的方法来更新页面或div,以便在提交表单后显示我的新数据。 Below is my code 下面是我的代码

$(function() {
    $('#form').submit(function(e) {

        var data = $(this).serialize();

        // Stop the form actually posting
        e.preventDefault();

        // Send the request
        $.ajax({
            type: "POST",
            url: "submit.php",
            data: data,
            cache: false,
            success: function(html){
                $('textarea#joke').val('');
            }
        });
    });
});

You are very close, just use html() method or text() depend on your needs, for your example I think text is better, since you want put text into textarea 您非常接近,根据需要使用html()方法或text(),例如,我认为文本更好,因为您希望将文本放入textarea

success: function(html){
                $('textarea#joke').text(html);
            }

but if you want put some html into custom div do 但是,如果您想在自定义div中放入一些html,

success: function(html){
                    $('#custom-div').html(html);
                }

Suppose from the submit.php, you are returning some value as status = true if form submitted suceessfully else status = false 假设从submit.php返回一些值,如果表单成功提交,则返回status = true ,否则status = false

then in your ajax code, you can use it as 然后在您的ajax代码中,您可以将其用作

success: function(html){
  if(html.status == true)
    $('textarea#joke').html('Form submitted succcessfully');
  else
    $('textarea#joke').html('ERROR!');
}

OR 要么

success: function(html){
    $('textarea#joke').val(html.status);
}

This would update the div content of $('textarea#joke') 这将更新$('textarea#joke')的div内容

Hope this would help you. 希望这对您有帮助。

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM