douhuang2282 2013-03-25 15:21
浏览 46
已采纳

Jquery .post然后使用.show和.hide

So I'm learning JQuery and I'm stuck on this:

I have a page that displays a HTML table and inside that table I want to have a cell that can be updated via a dropdown menu, so you click on edit, the current value disappears and dropdown menu appears, and when the value is changed the database is updated and the new value is displayed. (with the menu disappearing)

The problem seem to be putting the .text and .show inside the data callback function - if I alert the data it is returning the correct data from the PHP file, and if I comment out the .post line and replace the (data) with ("test_text") it replaces the menu as I want it to.

Hopefully my question is well enough written to make sense, thanks.

Here's the code

$('.cat_color_hide_rep').hide();
$('.act_status_dropD').click(function () {
    var record_id = $(this).parents('tr').find('.record_id').text()
    $(this).parents('tr').find('.cat_color_hide_rep').show();
    $(this).parents('tr').find('.cat_color_show_rep').hide();
});
$('.cat_color_hide_rep').change(function () {
    var record_id = $(this).parents('tr').find('.record_id').text()
    $(this).parents('tr').find('.cat_color_hide_rep').hide();
    $.post('TEST_ajax_rep_list_status.php', {
        ID: record_id
    }, function (data) {
        $(this).parents('tr').find('.cat_color_show_rep').text(data);
        $(this).parents('tr').find('.cat_color_show_rep').show();
        alert(data); // for testing
    });
});
  • 写回答

3条回答 默认 最新

  • dongzhu7329 2013-03-25 15:26
    关注

    You can not access the $(this) inside the $.post function.

    You can do this before the $.post:

    var that = this;
    

    And inside the post, do this:

    $(that).parents('tr').find('.cat_color_show_rep').text(data);
    $(that).parents('tr').find('.cat_color_show_rep').show();
    

    This would be your resulting code:

    $('.cat_color_hide_rep').hide();
    $('.act_status_dropD').click(function () {
        var record_id = $(this).parents('tr').find('.record_id').text()
        $(this).parents('tr').find('.cat_color_hide_rep').show();
        $(this).parents('tr').find('.cat_color_show_rep').hide();
    });
    $('.cat_color_hide_rep').change(function () {
        var record_id = $(this).parents('tr').find('.record_id').text()
        $(this).parents('tr').find('.cat_color_hide_rep').hide();
    
        /** ADDED LINE **/
        var that = this;
    
        $.post('TEST_ajax_rep_list_status.php', {
            ID: record_id
        }, function (data) {
    
            /** CHANGED LINES **/
            $(that).parents('tr').find('.cat_color_show_rep').text(data);
            $(that).parents('tr').find('.cat_color_show_rep').show();
    
            alert(data); // for testing
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line