Javascript 使用基于文本的Jquery选择选项 詹姆斯 约翰
出于某种原因,我可以执行Javascript 使用基于文本的Jquery选择选项 詹姆斯 约翰,javascript,jquery,Javascript,Jquery,出于某种原因,我可以执行$('select').val('something other'),因为value属性用于其他内容。为了避免弄乱当前的工作内容,如何基于文本值选择元素?表示我已经得到了值James和John您可以使用下面的 <select> <option value="something else">James</option> <option value="something else">John</option>
$('select').val('something other')
,因为value属性用于其他内容。为了避免弄乱当前的工作内容,如何基于文本值选择元素?表示我已经得到了值James
和John
您可以使用下面的
<select>
<option value="something else">James</option>
<option value="something else">John</option>
</select>
这将为您提供文本您可以使用下面的
<select>
<option value="something else">James</option>
<option value="something else">John</option>
</select>
这将为您提供文本
$(“选择选项”).filter(函数(){
return$(this.text()=='John';
}).prop('selected',true)
詹姆斯
约翰
$(“选择选项”).filter(函数(){
return$(this.text()=='John';
}).prop('selected',true)
詹姆斯
约翰
$('select').find('option[value=“something other”]').prop(“selected”,true)
$('select').find('option[value=“something other”]').prop(“selected”,true)
尝试使用$('select:selected').text()代码>尝试使用$(“选择:选定”).text()代码>
var selectText=函数(元素,文本){
如果(元素和文本){
元素find('option')。每个(函数(){
var,该值=$(此值);
如果(that.text()==text){
attr('selected','selected');
}
});
}
}
选择文本($('目标'),'约翰')代码>
詹姆斯
约翰
var selectText=函数(元素,文本){
如果(元素和文本){
元素find('option')。每个(函数(){
var,该值=$(此值);
如果(that.text()==text){
attr('selected','selected');
}
});
}
}
选择文本($('目标'),'约翰')代码>
詹姆斯
约翰
使用contains
通过如下文本选择选项
堆栈示例:
$(函数(){
var text=“John”;
$(“选择选项:包含(“+text+”)).attr('selected','selected');
});代码>
詹姆斯
约翰
使用contains
通过如下文本选择选项
堆栈示例:
$(函数(){
var text=“John”;
$(“选择选项:包含(“+text+”)).attr('selected','selected');
});代码>
詹姆斯
约翰
提交表单或获取选择值时,每个选项上的值必须唯一。提交表单或获取选择值时,每个选项上的值必须唯一。