代码之家  ›  专栏  ›  技术社区  ›  ntan

一旦更改,查找此父级

  •  2
  • ntan  · 技术社区  · 15 年前

    我有几个选择框(至少5个)具有以下结构

    <li>
       <div>
          <select name="sb[]">...</select>
       </div>
    </li>
    

    当sb更改时,我想让ajax调用传递选定的值,并用从ajax接收的HTML替换父li的内容。

    我尝试了以下方法

    onchange="
        $.get('file.php', { action: 'dothis'}, function(html) { 
          $(this).parent('li').html(html);
        });
    "
    

    但不起作用

    任何帮助

    2 回复  |  直到 6 年前
        1
  •  6
  •   jAndy    15 年前
    $('select[name="sb[]"]').change(function(){
       var $this = $(this);
       $.get('file.php', { action: 'dothis'}, function(html) { 
          $this.closest('li').html(html);
        });
    })
    
        2
  •  1
  •   Raphael Schweikert miya    15 年前

    如果您真的非常想这样做,您仍然可以在onchange属性中这样做。问题是(正如所指出的)双重的:首先,Ajax回调中的这个与变更事件处理程序中的这个不再相同,其次,应该使用closest()或parents():

    onchange="
        var select = $(this);
        $.get('file.php', { action: 'dothis'}, function(html) { 
          select.closest('li').html(html);
        });
    "
    

    onchange="
        $.get('file.php', { action: 'dothis'}, $.proxy(function(html) { 
          $(this).closest('li').html(html);
        }, this));
    "