jQuery – 通过文本描述设置select控件的选定值

我有一个select控制,并在一个JavaScriptvariables我有一个文本string。

使用jQuery我想将select控件的选定元素设置为具有文本描述的项目(而不是我没有的值)。

我知道设定它的价值是相当微不足道的。 例如

$("#my-select").val(myVal); 

但是我通过文字描述有点困难。 我想这肯定是一种从文字描述中获得价值的方法,但是我的大脑也是星期五下午能够解决的。

鉴于这个HTML:

 <select> <option value="0">One</option> <option value="1">Two</option> </select> 

通过描述selectjQuery v1.6 +:

 var text1 = 'Two'; $("select option").filter(function() { //may want to use $.trim in here return $(this).text() == text1; }).prop('selected', true); 

jQuery版本低于1.6并且大于或等于1.4的说明select : https : //stackoverflow.com/a/3644500/31532

 var text1 = 'Two'; $("select option").filter(function() { //may want to use $.trim in here return $(this).text() == text1; }).attr('selected', true); 

请注意,尽pipe此方法适用于1.6以上但小于1.9的版本,但自1.6版以来已弃用。 它不会在jQuery 1.9 +中工作

按以前版本的描述进行select:

val()应该处理这两种情况。 你没有看到吗?

例如:

 $('select').val('1'); // selects "Two" $('select').val('Two'); // also selects "Two" 

我没有testing过这个,但这可能适合你。

 $("select#my-select option") .each(function() { this.selected = (this.text == myVal); }); 

试试这个…select文本myText的选项

 $("#my-Select option[text=" + myText +"]").attr("selected","selected"); 

我这样做(jQuery 1.9.1)

 $("#my-select").val("Dutch").change(); 

注意:不要忘记改变(),我不得不search到,因为那:)

 $("#myselect option:contains('YourTextHere')").val(); 

将返回包含您的文本描述的第一个选项的值。 testing这个和工作。

为了避免所有的jQuery版本的复杂性,我老实推荐使用这些非常简单的JavaScript函数之一…

 function setSelectByValue(eID,val) { //Loop through sequentially// var ele=document.getElementById(eID); for(var ii=0; ii<ele.length; ii++) if(ele.options[ii].value==val) { //Found! ele.options[ii].selected=true; return true; } return false; } function setSelectByText(eID,text) { //Loop through sequentially// var ele=document.getElementById(eID); for(var ii=0; ii<ele.length; ii++) if(ele.options[ii].text==text) { //Found! ele.options[ii].selected=true; return true; } return false; } 

我知道这是一个旧的post,但我不能使用jQuery 1.10.3和上面的解决scheme通过文本select它。 我结束了使用下面的代码(spoulson的解决scheme的变体):

  var textToSelect = "Hello World"; $("#myDropDown option").each(function (a, b) { if ($(this).html() == textToSelect ) $(this).attr("selected", "selected"); }); 

希望它可以帮助别人。

  $("#Test").find("option:contains('two')").each(function(){ if( $(this).text() == 'two' ) { $(this).attr("selected","selected"); } }); 

if语句与“two”和“two three”完全匹配将不匹配

这条线路工作:

 $("#myDropDown option:contains(myText)").attr('selected', true); 

1.7+最简单的方法是:

 $("#myDropDown option:text=" + myText +"").attr("selected", "selected"); 

1.9+

 $("#myDropDown option:text=" + myText +"").prop("selected", "selected"); 

testing和工作。

看看jQuery的select框插件

 selectOptions(value[, clear]): 

使用string作为参数$("#myselect2").selectOptions("Value 1"); ,或正则expression式$("#myselect2").selectOptions(/^val/i);

您也可以清除已经select的选项: $("#myselect2").selectOptions("Value 2", true);

只是在一个侧面说明。 我select的值没有被设置。 我已经在网上search了。 实际上,我必须从Web服务callback后select一个值,因为我从中获取数据。

 $("#SelectMonth option[value=" + DataFromWebService + "]").attr('selected', 'selected'); $("#SelectMonth").selectmenu('refresh', true); 

所以select器的刷新是我唯一缺less的东西。

我发现,通过使用attr你将最终select多个选项,当你不想 – 解决scheme是使用prop

$("#myDropDown option:text=" + myText +"").prop("selected", "selected");

我上面的例子有一个问题,问题是由于我的select框的值预填充了6个字符的固定长度的string,但传入的参数不是固定长度。

我有一个rpad函数,它将正确填充一个string,指定的长度和指定的字符。 所以,填充参数后,它的工作。

 $('#wsWorkCenter').val(rpad(wsWorkCenter, 6, ' ')); function rpad(pStr, pLen, pPadStr) { if (pPadStr == '') {pPadStr == ' '}; while (pStr.length < pLen) pStr = pStr + pPadStr; return pStr; } 
  $('#theYear').on('change', function () { FY = $(this).find('option:selected').text(); $('#theFolders').each(function () { $('option:not(:contains(' + FY + '))', this).hide(); }); $('#theFolders').val(0); }); $('#theYear').on('mousedown', function () { $('#theFolders option').show().find('option:contains("Select")', this).attr('selected', 'selected'); }); 

这里是非常简单的方法。 PLZ使用它

 $("#free").val("y").change(); 

获取select框的孩子; 通过它们循环; 当你find你想要的那个时,把它设置为select的选项; 返回false来停止循环。

这个被接受的答案似乎不正确,而.val('newValue')是正确的函数,试图检索一个select的名字不适合我,我不得不使用id和classname来获取我的元素

这是一个简单的select。 只需设置您的列表选项,然后将其文本设置为选定的值:

 $("#ddlScheduleFrequency option").selected(text("Select One..."));