javascript - 通过 JQuery 在选择中选择选项

标签 javascript jquery html html-select

我有以下选项可供选择:

<select>
     <option value="1">A</option>
     <option value="2">B</option>
     <option value="3">C</option>
</select>

我不知道这些值是什么,但在我的 JQuery 代码中,我想选择选项二,因为我知道文本是 B(但别无其他)。

如何实现?

最佳答案

您可以使用 :contains()选择器:

$('option:contains("B")').prop('selected', true);

或者:

$('option')
    .filter(function() {
        return this.text == 'B'; 
    })
    .prop('selected', true);

关于javascript - 通过 JQuery 在选择中选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10715466/

相关文章:

javascript - 使用javascript获取上传的图像尺寸

javascript - 甜蜜页面 jquery 上一个/下一个按钮

javascript - 更改全日历的 View 不保留事件

javascript - 如何禁用 jQuery UI 弹出窗口上的按钮

html - 如何让元素忽略边距?

javascript - 选择框高度在 chrome 中不起作用

javascript - 使用 $cordova Oauth 问题登录

javascript - 你如何防止 javascript 中的 Math.random() 多次选择相同的数字?

javascript - 使用 JavaScript 移动 html td 元素

html - 在 lion 中禁用 webview 的橡皮筋滚动