jQuery-根据属性在下拉列表中获取所选选项之后的下一个选项的值

jQuery - Get value of the next option in a dropdown after the selected one based on attribute

本文关键字:选项 之后 下一个 获取 属性 下拉列表 jQuery-      更新时间:2023-09-26

HTML输出

<option task_estimated_hours="100" value="1"> - Parent Task</option>
<option task_estimated_hours="50" value="2"> -  - Child task</option>
<option task_estimated_hours="50" value="3"> -  - Child task</option>

jQuery代码:

$('#dropDownId option:selected').next().val()

上面显然会给我选择一个选项后的下一个选项的value,但不是基于属性task_estimated_hours。以下内容似乎不起作用。上面写着TypeError: $(...).attr(...).next is not a function

$('#dropDownId option:selected').attr('task_estimated_hours').next().val();

如何根据传递的属性获取下一个选项的值?

试试这个:-

$('#dropDownId option:selected').next().attr('task_estimated_hours');

附带说明:-使用HTML5自定义data-*属性,即使用data-task_estimated_hours 代替task_estimated_hours

Fiddle

这可能会帮助您

HTML:

<select id="dropDownId">
<option task_estimated_hours="10" value="1">1 option</option>
<option task_estimated_hours="20" value="2" selected="selected">2 option</option> 
<option value="3" >3 option</option>
<option task_estimated_hours="40" value="4">4 option</option>
</select>

jQuery:

alert($('#dropDownId option:selected').nextAll("option[task_estimated_hours]:first").attr("task_estimated_hours"));

Fiddle:

$('select option:selected').next('option[task_estimated_hours]').val();