jQuery 如何使用jQuery查找具有特定值的选项标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5128527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:36:25  来源:igfitidea点击:

How to find an option tag with a specific value using jQuery

jqueryfindoptionselected

提问by esviko

I'm trying to set an option with a specific value as selected (using jQuery). I've got a string:

我正在尝试使用选定的特定值设置一个选项(使用 jQuery)。我有一个字符串:

var data = '<select><option value="1">A</option><option value="2">B</option><option value="3">C</option></select>';

Now I'm trying to find the option tag with value=2 and set it as selected

现在我试图找到 value=2 的选项标签并将其设置为选中

$(data).find('option[value=2]').attr('selected','selected');

It's not working :-( ... I also tried:

它不起作用:-(...我也试过:

$(data).find('option').each(function(){
if($(this).val()==2){
    $(this).attr('selected','selected');
}
});

wich did not work either... Is there anbody out there who can help?

这也不起作用......有没有人可以提供帮助?

回答by user113716

Your code works fine, but of course you need to append it to the DOM to see the result. Here I used the appendTo()[docs]method.

您的代码工作正常,但当然您需要将其附加到 DOM 以查看结果。这里我使用了appendTo()[docs]方法。

Example:http://jsfiddle.net/cqhGH/

示例:http : //jsfiddle.net/cqhGH/

 // --v---------------make sure the DOM is loaded
$(function() {
    var data = '<select><option value="1">A</option><option value="2">B</option><option value="3">C</option></select>';

    $(data).appendTo('body').find('option[value=2]').attr('selected','selected');
});

Though an easier way is to use the val()[docs]method.

虽然更简单的方法是使用val()[docs]方法。

Example:http://jsfiddle.net/cqhGH/1/

示例:http : //jsfiddle.net/cqhGH/1/

$(function() {
    var data = '<select><option value="1">A</option><option value="2">B</option><option value="3">C</option></select>';

    $(data).appendTo('body').val(2);
});

回答by esviko

@patrick dw I think I have to be more specific :-) I'm trying to build some new table rows and append them to a table. The table looks like:

@patrick dw 我想我必须更具体:-) 我正在尝试构建一些新的表行并将它们附加到表中。该表看起来像:

<table id="my_id">
    <tbody>
    </tbody>
</table>

The table rows wich I want to append have a td with a select tag. The select tags in every row are the same except for the 'selected' attribute wich depends on the value.

我想附加的表行有一个带有选择标签的 td。除了“selected”属性取决于值之外,每一行中的选择标记都是相同的。

var new_tr = new Array();

var some_values = new Array(3,7,15);

var data = '<select><option value="1">A</option>...<option value="26">Z</option></select>';

$.each(some_values, function(i,j){
   $(data).find('option[value='+j+']').attr('selected','selected'); // this is the row wich does not work
   new_tr.push('<tr><td>'+data+'</td></tr>');
});

$('#my_id > tbody').append(new_tr.join(''));

I guess data needs to be an object, not a string...

我猜数据需要是一个对象,而不是一个字符串......