我需要检查a <select>是否有一个文本等于特定值的选项.
例如,如果有<option value="123">abc</option>,我会寻找"abc".
有选择器吗?
我正在寻找类似于$('#select option[value="123"]');文本的东西.
Har*_*til 309
这有助于:
$('#test').find('option[text="B"]').val();
Run Code Online (Sandbox Code Playgroud)
这将为您提供文本选项,B而不是包含文本的选项B.希望这可以帮助
对于最新版本的jQuery,上面的方法不起作用.如下面的Quandary评论,这适用于jQuery 1.9.1:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();
Run Code Online (Sandbox Code Playgroud)
SLa*_*aks 132
您可以使用:contains()选择器选择包含特定文本的元素.
例如:
$('#mySelect option:contains(abc)')
Run Code Online (Sandbox Code Playgroud)
要检查给定<select>元素是否具有此选项,请使用以下.has()方法:
if (mySelect.has('option:contains(abc)').length)
Run Code Online (Sandbox Code Playgroud)
要查找<select>包含此类选项的所有s,请使用:has()选择器:
$('select:has(option:contains(abc))')
Run Code Online (Sandbox Code Playgroud)
Dr.*_*ius 30
之前的建议都没有在jQuery 1.7.2中为我工作,因为我试图根据文本框中的值设置列表的选定索引,并且一些文本值包含在多个选项中.我最终使用了以下内容:
$('#mySelect option:contains(' + value + ')').each(function(){
if ($(this).text() == value) {
$(this).attr('selected', 'selected');
return false;
}
return true;
});
Run Code Online (Sandbox Code Playgroud)
Jay*_*hil 17
我面临同样的问题,下面是工作代码:
$("#test option").filter(function() {
return $(this).text() =='Ford';
}).prop("selected", true);
Run Code Online (Sandbox Code Playgroud)
演示:http://jsfiddle.net/YRBrp/83/
小智 14
这对我有用: $("#test").find("option:contains('abc')");
小智 12
这是下拉列表中选择文本的最佳方法.
$("#dropdownid option:contains(your selected text)").attr('selected', true);
Run Code Online (Sandbox Code Playgroud)
小智 5
I tried a few of these things until I got one to work in both Firefox and IE. This is what I came up with.
$("#my-Select").val($("#my-Select" + " option").filter(function() { return this.text == myText }).val());
Run Code Online (Sandbox Code Playgroud)
another way of writing it in a more readable fasion:
var valofText = $("#my-Select" + " option").filter(function() {
return this.text == myText
}).val();
$(ElementID).val(valofText);
Run Code Online (Sandbox Code Playgroud)
Pseudocode:
$("#my-Select").val( getValOfText( myText ) );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
219928 次 |
| 最近记录: |