【问题标题】:jQuery remove selected option from thisjQuery从中删除选定的选项
【发布时间】:2011-01-30 10:53:02
【问题描述】:

第一次在这里发帖,我很平静:)我已经搜索过,但找不到我想要的。

我正在尝试操作选择框的选定选项。有人可以解释一下为什么会这样吗:

$('#some_select_box').click(function() {
  $('#some_select_box option:selected').remove();
});

但这不是:

$('#some_select_box').click(function() {
  $('this option:selected').remove();
});

我只想使用“this”而不是拼出选择框的 id - 有人可以为我指出正确的语法方向吗?这让我发疯,因为它看起来应该非常简单。而且我确定它是给某人的,但不是我,因为它是一天的结束,我脑筋急转弯......非常感谢任何指针。

干杯

【问题讨论】:

    标签: jquery select this option selected


    【解决方案1】:

    this 不是 CSS 选择器。 您可以通过将 this 的 id 作为上下文传递来避免拼写它:

    $('option:selected', this).remove();
    

    http://api.jquery.com/jQuery/

    【讨论】:

    • 太好了,谢谢...看起来还有很多 jquery 需要学习。不知道这种语法。非常感谢:)
    【解决方案2】:
     $('#some_select_box').click(function() {
         $(this).find('option:selected').remove();
     });
    

    使用find 方法。

    【讨论】:

    • 如何在没有jQuery的情况下使用纯javascript实现
    【解决方案3】:

    这应该可以解决问题:

    $('#some_select_box').click(function() {
      $('option:selected', this ).remove();
    });
    

    【讨论】:

      【解决方案4】:

      这个比较简单

      $('#some_select_box').find('option:selected').remove().end();
      

      【讨论】:

        【解决方案5】:

        $('#some_select_box option:selected').remove();

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-15
          • 1970-01-01
          • 2011-09-28
          • 2019-04-18
          • 1970-01-01
          • 2012-02-25
          • 1970-01-01
          相关资源
          最近更新 更多