<select>
<option value="something else">James</option>
<option value="something else">John</option>
</select>
出于某种原因,我可以执行 $('select').val('something else')
因为 value 属性用于其他内容。为了避免搞乱当前的工作,如何根据文本值选择元素?说我已经获得了值 James
和 John
。
最佳答案
$("select option").filter(function() {
return $(this).text() == 'John';
}).prop('selected', true)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="something else">James</option>
<option value="something else">John</option>
</select>
Description: Reduce the set of matched elements to those that match the selector or pass the function's test.
关于javascript - 基于文本使用 Jquery 选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37267611/