尝试使用ID标识符选择下一个选项的下拉列表,但无济于事。
这是代码:
$('#chapter option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected')
尝试使用ID标识符选择下一个选项的下拉列表,但无济于事。
这是代码:
$('#chapter option:selected', 'select').removeAttr('selected').next('option').attr('selected', 'selected')
我假设#chapter
是<select>
或其祖先元素之一。
如果是这种情况,请删除上下文参数。
$('#chapter option:selected').removeAttr('selected')
.next('option').attr('selected', 'selected');
您之前的做法实际上是这样的:
$('select').find('#chapter option:selected').removeAttr(...
这段代码是在查找一个位于 <select>
元素内部的带有 option:selected
属性的元素,该元素的父级元素具有 chapter
ID。
编辑: 这个答案仅关注为什么选择不起作用。更好的方法是使用@Andy E的答案来完成最终任务。
我建议将那个答案设置为接受的答案。
selected
属性,则无需删除它。 - Andy E<select>
元素具有 multiple 属性,否则在将 <option>
元素添加到其兄弟元素之一时,无需删除选定的属性。 假设 patrick dw's answer 是正确的(因为您已经接受了它),您可以通过将 DOM 返回到其根源来将此代码重构为更高效的代码:$("#chapter")[0].selectedIndex++;
$("#chapter")[0].selectedIndex++
- Raynos