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

jQuery选择列表删除所有选项

  •  35
  • Richbits  · 技术社区  · 17 年前

    我有一个 <select>

    $("#selectId").length = 0;
    

    但这似乎没有效果。

    <head> 我的HTML文件?

    7 回复  |  直到 11 年前
        1
  •  49
  •   David Hedlund    17 年前

    这可以做到:

    $('#selectId').html('');
    
        2
  •  27
  •   AKX Bryan Oakley    17 年前

    $("#selectId option").remove(); 
    
        3
  •  11
  •   Dr. Rajesh Rolen    14 年前

    $("#selectId").empty(); 为我工作。

    这 $("#selectId option").remove();

        4
  •  6
  •   Community Mohan Dere    9 年前

    实现这一目标的最快方法:

    $("#selectId").empty();
    

    以下是一些测试: http://jsperf.com/find-remove-vs-empty

    我不认为这些测试是我的功劳。 请查看此堆栈溢出问题: How do you remove all the options of a select box and then add one option and select it with jQuery?

        5
  •  1
  •   lance    17 年前
    $("#selectId").options.length = 0;
    

    这实际上不会像写的那样工作,因为它使用了jQuery选择器。不过,如果在选择列表上使用直接的DOM getElementById,它就可以工作。AKX或d.的回答都会让你正确。

        6
  •  1
  •   RwwL    17 年前

    如果没有jquery:

    javascript SELECT remove()

        7
  •  0
  •   Ahsan Aftab    8 年前

    $('#FeatureId').multiselect(“全部取消选择”,false).multiselect(“刷新”);