我正在尝试使用 jquery val() 在选择下拉菜单上设置所选属性。我只有我想要选择的元素的标题(又名innerhtml),但没有值。 例如:
状态下拉。我希望选择“纽约”选项。我想写一个jquery语句来实现
$("#pull-down").val([get value of the option tag which has "New York" as its innerhtml])
这可能吗?
最佳答案
您可以使用filter
方法。
$("#pull-down").val(function(){
return $('#selectElement option').filter(function(){
return $(this).text() === 'New York';
}).val();
});
如果 #pull-down
是一个 select 元素,并且包含 New York
的 option 元素是它的子元素,则可以使用 prop
方法。
$('#pull-down option').filter(function(){
return $(this).text() === 'New York';
}).prop('selected', true);
关于jquery - 使用jquery val()及其innerhtml设置html select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14696000/