jQuery Set选择索引

255 jquery jquery-selectors

我有一个选择框:

<select id="selectBox">
  <option value="0">Number 0</option>
  <option value="1">Number 1</option>
  <option value="2">Number 2</option>
  <option value="3">Number 3</option>
  <option value="4">Number 4</option>
  <option value="5">Number 5</option>
  <option value="6">Number 6</option>
  <option value="7">Number 7</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我想根据它选择的索引将其中一个选项设置为"已选择".

例如,如果我尝试设置"3号",我正在尝试这样做:

$('#selectBox')[3].attr('selected', 'selected');
Run Code Online (Sandbox Code Playgroud)

但这不起作用.如何使用jQuery基于其索引设置选项?

谢谢!

Mar*_*arc 443

注意:答案取决于jQuery 1.6.1+

$('#selectBox :nth-child(4)').prop('selected', true); // To select via index
$('#selectBox option:eq(3)').prop('selected', true);  // To select via value
Run Code Online (Sandbox Code Playgroud)

感谢您的评论,.get因为它返回一个DOM元素而不是jQuery元素,因此无法工作.请记住,.eq如果您愿意,也可以在选择器外部使用该功能.

$('#selectBox option').eq(3).prop('selected', true);
Run Code Online (Sandbox Code Playgroud)

如果要使用该,也可以更简洁/可读,而不是依赖于选择特定索引:

$("#selectBox").val("3");
Run Code Online (Sandbox Code Playgroud)

注意: .val(3)对于此示例也适用,但非数字值必须是字符串,因此我选择了一个字符串以保持一致性.
(例如<option value="hello">Number3</option>要求你使用.val("hello"))

  • @mikejones - 这是设计的.当您以编程方式设置所选选项时,您可能并不总是希望触发绑定更改事件(例如在页面加载事件上).要解决该问题,请将jquery留给开发人员来决定.在你的情况下,你可以简单地调用$('#selectBox').change(); 在你调用$('#selectBox')之后.val("3"); (28认同)
  • +1 - 第二个选项对我有用.约翰库格曼选择的答案在IE7中对我不起作用. (7认同)
  • 我认为值得一提的是post-jQuery 1.6.1,修改元素布尔属性的推荐方法是`.prop()`而不是`.attr()`. (3认同)
  • 可悲的是,这些方法都没有为我发起change()事件.我有$('#selectBox').change(function(){alert('changed')}); (2认同)
  • 第一个选项是0还是1? (2认同)

dno*_*oxs 114

这也可能有用,所以我想我会在这里添加它.

如果要根据项目的值而不是该项目的索引选择值,则可以执行以下操作:

您的选择列表:

<select id="selectBox">
    <option value="A">Number 0</option>
    <option value="B">Number 1</option>
    <option value="C">Number 2</option>
    <option value="D">Number 3</option>
    <option value="E">Number 4</option>
    <option value="F">Number 5</option>
    <option value="G">Number 6</option>
    <option value="H">Number 7</option>
</select>
Run Code Online (Sandbox Code Playgroud)

jquery:

$('#selectBox option[value=C]').attr('selected', 'selected');
Run Code Online (Sandbox Code Playgroud)

$('#selectBox option[value=C]').prop('selected', true);
Run Code Online (Sandbox Code Playgroud)

所选项目现在为"2号".

  • 请注意`.attr('selected','selected')`不应该在同一个select上多次使用,因为有些浏览器会混淆(在某些情况下,他们开始在dom中标记几个项目为`selected =选择"`.如果你需要更频繁地更改选定的值(例如点击一下按钮),请按照马克的建议使用`.prop('selected',true)` - 只是有很多痛苦 - 浪费时间 - 因为这个问题! (5认同)

Chr*_*sma 64

试试这个:

$("#selectBox").val(3);
Run Code Online (Sandbox Code Playgroud)

另外,看看这是否有助于你:

http://elegantcode.com/2009/07/01/jquery-playing-with-select-dropdownlistcombobox/

  • 这使得从一个元素复制到另一个元素也很简单! (2认同)

小智 49

更简单:

$('#selectBox option')[3].selected = true;
Run Code Online (Sandbox Code Playgroud)

  • 这个答案给了我所需要的..当尝试按名称选择 $('select[name="select_name"] option)[index].selected=true; 时,我缺少选择器的“选项”部分 (2认同)

Mo *_*ami 16

# Set element with index
$("#select option:eq(2)").attr("selected", "selected");

# Set element by text
$("#select").val("option Text").attr("selected", "selected");
Run Code Online (Sandbox Code Playgroud)

当您想要使用顶部方式选择集合时,可以使用
$('#select option').removeAttr('selected');删除之前的选择.

# Set element by value
$("#select").val("2");

# Get selected text
$("#select").children("option:selected").text();  # use attr() for get attributes
$("#select option:selected").text(); # use attr() for get attributes 

# Get selected value
$("#select option:selected").val();
$("#select").children("option:selected").val();
$("#select option:selected").prevAll().size();
$("option:selected",this).val();

# Get selected index
$("#select option:selected").index();
$("#select option").index($("#select option:selected"));

# Select First Option
$("#select option:first");

# Select Last Item
$("#select option:last").remove();


# Replace item with new one
$("#select option:eq(1)").replaceWith("<option value='2'>new option</option>");

# Remove an item
$("#select option:eq(0)").remove();
Run Code Online (Sandbox Code Playgroud)


Ano*_*ous 11

重要的是要理解,val()对于一个select元素,返回所选选项的值,而不是像selectedIndexjavascript中那样返回元素的数量.

要选择该选项,value="7"您只需使用:

$('#selectBox').val(7); //this will select the option with value 7.
Run Code Online (Sandbox Code Playgroud)

要取消选择该选项,请使用空数组:

$('#selectBox').val([]); //this is the best way to deselect the options
Run Code Online (Sandbox Code Playgroud)

当然,您可以选择多个选项*:

$('#selectBox').val([1,4,7]); //will select options with values 1,4 and 7
Run Code Online (Sandbox Code Playgroud)

*但是,要选择多个选项,您的<select>元素必须具有MULTIPLE属性,否则它将无法工作.


hel*_*bus 11

我一直有道具('选择')的问题,以下一直对我有用:

//first remove the current value
$("#selectBox").children().removeAttr("selected");
$("#selectBox").children().eq(index).attr('selected', 'selected');
Run Code Online (Sandbox Code Playgroud)


Nic*_*ier 8

试试这个:

$('select#mySelect').prop('selectedIndex', optionIndex);
Run Code Online (Sandbox Code Playgroud)

最终,触发.change事件:

$('select#mySelect').prop('selectedIndex', optionIndex).change();
Run Code Online (Sandbox Code Playgroud)


And*_*rew 6

希望这可以帮助太多

$('#selectBox option[value="3"]').attr('selected', true);
Run Code Online (Sandbox Code Playgroud)


jac*_*kno 5

纯 javascript selectedIndex属性是正确的方法,因为它是纯 javascript 并且可以跨浏览器工作:

$('#selectBox')[0].selectedIndex=4;
Run Code Online (Sandbox Code Playgroud)

这是一个 jsfiddle 演示,有两个下拉菜单,使用一个下拉菜单设置另一个:

<select onchange="$('#selectBox')[0].selectedIndex=this.selectedIndex">
  <option>0</option>
  <option>1</option>
  <option>2</option>
</select>

<select id="selectBox">
  <option value="0">Number 0</option>
  <option value="1">Number 1</option>
  <option value="2">Number 2</option>
</select>
Run Code Online (Sandbox Code Playgroud)

如果您想要的是选项标签上的“selected”属性(这里是小提琴),您也可以在更改 selectedIndex 之前调用此方法:

$('#selectBox option').removeAttr('selected')
   .eq(this.selectedIndex).attr('selected','selected');
Run Code Online (Sandbox Code Playgroud)


小智 5

选择第三个选项

$('#selectBox').val($('#selectBox option').eq(2).val());
Run Code Online (Sandbox Code Playgroud)

jsfiddle上的示例