如何删除选择框的所有选项,然后添加一个选项并使用jQuery选择它?

时间:2020-03-05 18:48:47  来源:igfitidea点击:

使用核心jQuery,如何删除选择框的所有选项,然后添加一个选项并选择它?

我的选择框如下。

<Select id="mySelect" size="9" </Select>

编辑:以下代码有助于链接。但是,(在InternetExplorer中).val('whatever')没有选择添加的选项。 (我确实在.append和.val中都使用了相同的"值"。)

$('#mySelect').find('option').remove().end().append('<option value="whatever">text</option>').val('whatever');

编辑:试图使其模仿此代码,每当重置页面/窗体时,我都使用以下代码。该选择框由一组单选按钮填充。 .focus()距离更近,但是该选项没有像.selected =" true"一样被选中。我现有的代码没什么问题,我只是想学习jQuery。

var mySelect = document.getElementById('mySelect');
mySelect.options.length = 0;
mySelect.options[0] = new Option ("Foo (only choice)", "Foo");
mySelect.options[0].selected="true";

编辑:选定的答案接近我所需要的。这为我工作:

$('#mySelect').children().remove().end().append('<option selected value="whatever">text</option>') ;

但是,这两个答案都使我找到了最终解决方案。

解决方案

回答

$('#mySelect')
    .find('option')
    .remove()
    .end()
    .append('<option value="whatever">text</option>')
    .val('whatever')
;

回答

不确定确切的含义是"添加并选择一个",因为无论如何它都会被默认选中。但是,如果要添加多个,那就更有意义了。怎么样:

$('select').children().remove();
$('select').append('<option id="foo">foo</option>');
$('#foo').focus();

对"编辑"的回答:我们能否澄清"此选择框由一组单选按钮填充"的含义? <select>元素不能(合法地)包含<input type =" radio">元素。

回答

多亏了我收到的答案,我才能创建出类似我自己需要的东西。我的问题有点模棱两可。感谢跟进。我的最后一个问题通过在要选择的选项中包含" selected"来解决。

$(function() {
  $('#mySelect').children().remove().end().append('<option selected value="One">One option</option>') ; // clear the select box, then add one option which is selected
  $("input[name='myRadio']").filter( "[value='1']" ).attr( "checked", "checked" ); // select radio button with value 1
  // Bind click event to each radio button.
  $("input[name='myRadio']").bind("click",
                                  function() {
    switch(this.value) {
      case "1":
        $('#mySelect').find('option').remove().end().append('<option selected value="One">One option</option>') ;
        break ;
      case "2":
        $('#mySelect').find('option').remove() ;
        var items = ["Item1", "Item2", "Item3"] ; // Set locally for demo
        var options = '' ;
        for (var i = 0; i < items.length; i++) {
          if (i==0) {
            options += '<option selected value="' + items[i] + '">' + items[i] + '</option>';
          }
          else {
            options += '<option value="' + items[i] + '">' + items[i] + '</option>';
          }
        }
        $('#mySelect').html(options);   // Populate select box with array
        break ;
    } // Switch end
  } // Bind function end
                                 ); // bind end
}); // Event listener end
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>One<input  name="myRadio" type="radio" value="1"  /></label>
<label>Two<input name="myRadio"  type="radio" value="2" /></label>
<select id="mySelect" size="9"></select>

回答

$('#mySelect')
    .empty()
    .append('<option value="whatever">text</option>')
    .find('option:first')
    .attr("selected","selected")
;

回答

$('#mySelect')
    .empty()
    .append('<option selected="selected" value="whatever">text</option>')
;

回答

我在IE7中有一个错误(在IE6中可以正常工作),在该错误中,使用上述jQuery方法会清除DOM中的选择内容,但不会显示在屏幕上。使用IE Developer工具栏,我可以确认选择已被清除并包含新项目,但是即使我们无法选择,该项目在视觉上仍显示旧项目。

解决方法是使用标准的DOM方法/属性(如原始海报的原始属性)来清除,而不是jQuery仍使用jQuery添加选项。

$('#mySelect')[0].options.length = 0;