jquery - 使用jquery val()及其innerhtml设置html select

标签 jquery

我正在尝试使用 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/

相关文章:

Javascript jQuery json 每个

javascript - jquery 和 css 绝对居中

javascript - 颜色选择器值不与 aurelia 绑定(bind)

jquery - 在 webkit 浏览器刷新 $(document).ready 之后加载图像

javascript - 使用 .append() 将标题属性添加到动态选择选项

jquery/IE mousemove 事件随着工具提示消失而触发,但鼠标没有移动

javascript - 动态创建 Angular View

javascript - JQuery 选择由 2 个特定单词组成的类名

jquery - 固定定位不起作用

javascript - JS 函数序列如何使用 Promise