我需要哪个选择器通过其文本选择一个选项? [英] Which selector do I need to select an option by its text?

查看:86
本文介绍了我需要哪个选择器通过其文本选择一个选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查<select>是否具有文本等于特定值的选项.

I need to check if a <select> has an option whose text is equal to a specific value.

例如,如果有一个<option value="123">abc</option>,我会在寻找"abc".

For example, if there's an <option value="123">abc</option>, I would be looking for "abc".

是否有选择器可以做到这一点?

Is there a selector to do this?

我正在寻找与$('#select option[value="123"]');类似的内容,但需要输入文字.

Im looking for something similar to $('#select option[value="123"]'); but for the text.

推荐答案

这可能会有所帮助:

$('#test').find('option[text="B"]').val();

演示小提琴

这将为您提供文本为B的选项,而不是文本为包含B的选项.希望这会有所帮助

This would give you the option with text B and not the ones which has text that contains B. Hope this helps

对于jQuery的最新版本,以上操作无效.如下面的 Quandary 所述,这才适用于jQuery 1.9.1:

For recent versions of jQuery the above does not work. As commented by Quandary below, this is what works for jQuery 1.9.1:

$('#test option').filter(function () { return $(this).html() == "B"; }).val();

更新了小提琴

这篇关于我需要哪个选择器通过其文本选择一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆