Dan*_*man 526 javascript jquery
我有一个选择控件,在一个javascript变量中我有一个文本字符串.
使用jQuery我想将select控件的selected元素设置为具有我所拥有的文本描述的项目(而不是我没有的值).
我知道按值设置它是非常微不足道的.例如
$("#my-select").val(myVal);
Run Code Online (Sandbox Code Playgroud)
但是通过文字描述我有点难过.我想必须有一种从文本描述中获取价值的方法,但是我的大脑在星期五下午也可以解决它.
Cre*_*esh 750
鉴于此HTML:
var text1 = 'Two';
$("select option").filter(function() {
//may want to use $.trim in here
return $(this).text() == text1;
}).prop('selected', true);
Run Code Online (Sandbox Code Playgroud)
按jQuery v1.6 +的描述选择:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
<option value="0">One</option>
<option value="1">Two</option>
</select>
Run Code Online (Sandbox Code Playgroud)
按描述选择1.6以下且大于或等于1.4的jQuery版本: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);
Run Code Online (Sandbox Code Playgroud)
请注意,虽然此方法适用于1.6以上但小于1.9的版本,但自1.6以来已被弃用.它不适用于jQuery 1.9+.
按以前版本的说明选择:
val()
应该处理这两种情况.你没有看到它吗?
例如:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
<select>
<option value="0">One</option>
<option value="1">Two</option>
</select>
Run Code Online (Sandbox Code Playgroud)
spo*_*son 141
我没有测试过这个,但这可能对你有用.
$("select#my-select option")
.each(function() { this.selected = (this.text == myVal); });
Run Code Online (Sandbox Code Playgroud)
Jay*_*ett 90
试试这个......选择带有文本myText的选项
$("#my-Select option[text=" + myText +"]").attr("selected","selected");
Run Code Online (Sandbox Code Playgroud)
Era*_*cht 40
我是这样做的(jQuery 1.9.1)
$("#my-select").val("Dutch").change();
Run Code Online (Sandbox Code Playgroud)
注意:不要忘记更改(),我不得不搜索到因为那个:)
Rus*_*Cam 21
$("#myselect option:contains('YourTextHere')").val();
Run Code Online (Sandbox Code Playgroud)
将返回包含您的文字说明的第一个选项的值.经过测试并且有效.
Dav*_*ave 15
为了避免所有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;
}
Run Code Online (Sandbox Code Playgroud)
Los*_*ear 10
我知道这是一篇旧帖子,但我无法通过使用jQuery 1.10.3及上述解决方案的文本进行选择.我最终使用了以下代码(spoulson解决方案的变体):
var textToSelect = "Hello World";
$("#myDropDown option").each(function (a, b) {
if ($(this).html() == textToSelect ) $(this).attr("selected", "selected");
});
Run Code Online (Sandbox Code Playgroud)
希望它可以帮助某人.
Die*_*nue 10
这条线有效:
$("#myDropDown option:contains(myText)").attr('selected', true);
Run Code Online (Sandbox Code Playgroud)
$("#Test").find("option:contains('two')").each(function(){
if( $(this).text() == 'two' ) {
$(this).attr("selected","selected");
}
});
Run Code Online (Sandbox Code Playgroud)
if语句与"two"完全匹配,"two three"将不匹配
小智 6
1.7+的最简单方法是:
$("#myDropDown option:text=" + myText +"").attr("selected", "selected");
Run Code Online (Sandbox Code Playgroud)
1.9+
$("#myDropDown option:text=" + myText +"").prop("selected", "selected");
Run Code Online (Sandbox Code Playgroud)
经过测试和工作.
这是非常简单的方法。请使用它
$("#free").val("y").change();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1082772 次 |
最近记录: |