简体   繁体   English

在带有jQuery的下拉菜单中查找文本并将其设置为选定

[英]Finding text in Dropdown menu with Jquery and setting it to Selected

I'm trying to check if a particular text value exists within a dropdown menu and if it does I would like to set it's attribute to selected . 我正在尝试检查dropdown菜单中是否存在特定的文本值,如果确实存在,我想将其属性设置为selected

I've managed to write an if statement that checks to see if the text exists: 我设法编写了一个if语句,检查该文本是否存在:

var country = usr.location;
if ($('select[name="country"] option:contains(' + country + ')').length) {
        $(this).find('option:contains("' + country + '")').attr('selected', 'selected');
 }

However, the issue I've run into is targeting the option and setting it to selected . 但是,我遇到的问题是针对该option并将其设置为selected

This is a fiddle of my work so far: 到目前为止,这是我工作的一个小提琴:

http://jsfiddle.net/javacadabra/ha2qwph3/ http://jsfiddle.net/javacadabra/ha2qwph3/

any help is appreciated. 任何帮助表示赞赏。

EDIT: I've answered my own question but I'd be open to suggestions on ways in which my code could be improved. 编辑:我已经回答了我自己的问题,但我愿意就可以改进我的代码的方式提出建议。 thank you. 谢谢。

Correct way is, 正确的方法是

You have no scope for this in if-else condition if-else条件下,您无权执行this

var country = "Ireland";

if($('select[name="country"] option:contains(' + country + ')').length){
     //alert('found ' + country); 
    $('select[name="country"]  option:contains(' + country + ')').attr('selected', 'selected');
}

Demo 演示版

Even a better solution to make a cache of your select element like, 甚至更好的解决方案是对您的select element进行缓存,例如,

var country = "Ireland";
var $countrySelected=$('select[name="country"] option:contains(' + country + ')');
if($countrySelected.length){
    //alert('found ' + country); 
    $countrySelected.attr('selected', 'selected');
}

You can try this too: Demo 您也可以尝试以下方法: 演示

JS: JS:

var country = "Ireland";
$('select[name="country"] option').each(function(){
    if($(this).text().indexOf(country)>-1){
        $(this).attr('selected','selected');
    }
})

Actually, I've managed to find a solution myself after tinkering with the Fiddle. 实际上,在与Fiddle进行修补后,我自己设法找到了解决方案。 Here is the Code for anyone who may need to achieve the same thing: 以下是可能需要实现同一目标的任何人的代码:

var country = "Ireland"; var country =“爱尔兰”;

if($('select[name="country"] option:contains(' + country + ')').length){
     alert('found ' + country); 
    $('select[name="country"] option:contains(' + country + ')').attr('selected', 'selected');
}

You can dynamically add a country to a variable on change and then check for the existence, fix : http://jsfiddle.net/ha2qwph3/4/ 您可以在更改时动态地将国家/地区添加到变量中,然后检查其存在并进行修复: http : //jsfiddle.net/ha2qwph3/4/

$('#select').change(function(){
var country = $( "select option:selected" ).html();

if($('select[name="country"] option:contains(' + country + ')').length){
     alert('found ' + country); 

}
});

Please replace the below code 请替换以下代码

$(this).find('option:contains(' + country + ')').attr('selected', 'selected');

with

$('select option:contains(' + country + ')').attr('selected', 'selected');

Hope this helps you.Thank you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM