Javascript 另外,你可以将attr(“selected”,“selected”)链接到包装集而不是val(),这与CarolinaJay65的回答类似,回答是“设置值”而不是“获取值”不,我不是这个意思。我觉得我的评论有点不清楚,所以我把它删掉了。不过,我确实对你的答

Javascript 另外,你可以将attr(“selected”,“selected”)链接到包装集而不是val(),这与CarolinaJay65的回答类似,回答是“设置值”而不是“获取值”不,我不是这个意思。我觉得我的评论有点不清楚,所以我把它删掉了。不过,我确实对你的答,javascript,jquery,Javascript,Jquery,另外,你可以将attr(“selected”,“selected”)链接到包装集而不是val(),这与CarolinaJay65的回答类似,回答是“设置值”而不是“获取值”不,我不是这个意思。我觉得我的评论有点不清楚,所以我把它删掉了。不过,我确实对你的答案投了赞成票,因为它适用于我的情况。它可以通过值或文本(取决于调用的函数)来选择选择选项。例如:setSelectByValue(“香肠”、“2”);将在id为“香肠”的选择对象中找到并选择值为“2”的选项。@Neal Uh。。。它回答了问题?


另外,你可以将attr(“selected”,“selected”)链接到包装集而不是val(),这与CarolinaJay65的回答类似,回答是“设置值”而不是“获取值”不,我不是这个意思。我觉得我的评论有点不清楚,所以我把它删掉了。不过,我确实对你的答案投了赞成票,因为它适用于我的情况。它可以通过值或文本(取决于调用的函数)来选择选择选项。例如:setSelectByValue(“香肠”、“2”);将在id为“香肠”的选择对象中找到并选择值为“2”的选项。@Neal Uh。。。它回答了问题?不是在1.9+中,它没有。从1.6开始,您应该使用
.prop
来更改DOM属性,而不是
.attr
(用于HTML/DOM属性)。看,我将使用(“selected”,true)而不是“selected”的真实值@DanAtkinson本人也将这样做。与这个问题毫无关系。这是正确的答案-尽管。。。我看不出第二行的理由。应该按原样工作。希望我能多次投票。戒烟时间已经过了一个小时,现在我可以回家了。回答得好!简短而简单…这应该是最重要的…继续投票。这在我的情况下有效,谢谢!但这不是按OP请求的显示文本进行选择,而是按OP没有的值本身进行选择吗?必须对jQuery 1.12.3使用
prop('selected',true)
$("#my-select").val(myVal);
$("select#my-select option")
   .each(function() { this.selected = (this.text == myVal); });
$("#myselect option:contains('YourTextHere')").val();
$("#my-Select option[text=" + myText +"]").prop("selected", true);
selectOptions(value[, clear]): 
 $("#Test").find("option:contains('two')").each(function(){
     if( $(this).text() == 'two' ) {
        $(this).attr("selected","selected");
     }
 });
$('#wsWorkCenter').val(rpad(wsWorkCenter, 6, ' '));


function rpad(pStr, pLen, pPadStr) {
if (pPadStr == '') {pPadStr == ' '};
while (pStr.length < pLen)
    pStr = pStr + pPadStr;
return pStr; 
} 
$("#myDropDown option:text=" + myText +"").attr("selected", "selected"); 
$("#myDropDown option:text=" + myText +"").prop("selected", "selected"); 
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;
}
$("#SelectMonth option[value=" + DataFromWebService + "]").attr('selected', 'selected'); 
$("#SelectMonth").selectmenu('refresh', true);
      var textToSelect = "Hello World";

      $("#myDropDown option").each(function (a, b) {
            if ($(this).html() == textToSelect ) $(this).attr("selected", "selected");
        });
$("#myDropDown option:contains(myText)").attr('selected', true);
 $('#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');
});
$("#ddlScheduleFrequency option").selected(text("Select One..."));
$("#my-select").val("Dutch").change();
$("#free").val("y").change();
[...mySelect.options].forEach(o=> o.selected = o.text == 'Text C' )
<select name="0product_id[]" class="groupSelect" id="groupsel_0" onchange="productbuilder.update(this.value,0);">
    <option value="0" class="notag" id="id0_0">--Select--</option>
    <option class="notag" value="338" id="id0_338"  >Dual Promoter Puromycin Expression Plasmid - pSF-CMV-PGK-Puro  > £114.00</option>
    <option class="notag" value="282" id="id0_282"  >EMCV IRES Puromycin Expression Plasmid - pSF-CMV-EMCV-Puro  > £114.00</option>
    <option class="notag" value="265" id="id0_265"  >FMDV IRES Puromycin Expression Plasmid - pSF-CMV-FMDV-Puro  > £114.00</option>
    <option class="notag" value="101" id="id0_101"  >Puromycin Selection Plasmid - pSF-CMV-Ub-Puro AscI  > £114.00</option>
    <option class="notag" value="105" id="id0_105"  >Puromycin Selection SV40 Ori Plasmid - pSF-CMV-Ub-Puro-SV40 Ori SbfI  > £114.00</option></select>
$( document ).ready(function() {
      var text = "EMCV IRES Puromycin Expression Plasmid - pSF-CMV-EMCV-Puro  > £114.00";
      alert(text);
$("#groupsel_0 option").filter(function() {
  //may want to use $.trim in here
  return $(this).text() == text;
}).prop('selected', true);
});