【问题标题】:Refresh database information on page without refreshing the same page刷新页面上的数据库信息而不刷新同一页面
【发布时间】:2016-03-03 12:07:39
【问题描述】:

我有两种发布方法,一种是获取信息,另一种是向同一个数据库添加信息。 现在我希望其他发布方法在为其添加新数据后获取信息。 这是我的第一个 POST 方法:

$(".selectedMessageEmail").click(function(){
    var id = $(this).attr('id');
    $.post('messageSystem.php', {id, id}, function(result){
        $("#rightBody").html(result);
    });
    show = id;
});

这是我的第二个 POST 方法:

$("#replyButton").click(function(){
    var getValue = $.trim($("#sendMessageTextArea").val());
    if(getValue != ""){
        $.post('messageReplySystem.php', {replyMessagePost : reply_form.send_message_text_area.value, show : show}, function(resultTwo){
            $("#sendMessageTextArea").val("");
        });
    }
});

换句话说,如何只在一个 div 中显示更新的数据库信息,而不刷新整个页面,也不复制我的代码。

【问题讨论】:

  • 您可以将需要复制的代码包装在一个函数中,然后只使用该函数两次...无论如何我都会这样做。

标签: php jquery ajax post


【解决方案1】:

您可以创建一个更新 #rightBody 的函数,然后在两个实例中调用它。

var show;

function updateRightBody(id) {
    $.post('messageSystem.php', {id, id}, function(result){
        $("#rightBody").html(result);
    });
}

$(".selectedMessageEmail").click(function(){
    var id = $(this).attr('id');
    updateRightBody(id);
    show = id;
});

$("#replyButton").click(function(){
    var getValue = $.trim($("#sendMessageTextArea").val());
    if(getValue != ""){
        $.post('messageReplySystem.php', {replyMessagePost : reply_form.send_message_text_area.value, show : show}, function(resultTwo){
            $("#sendMessageTextArea").val("");
        });
        updateRightBody(show);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2011-12-22
    • 1970-01-01
    • 2011-04-28
    • 2015-03-04
    相关资源
    最近更新 更多