jquery - 使用 jQuery 将选择框设置为其默认选定索引

标签 jquery html

好的,我有一个选择框

<select id="txtPriority" style="color:#008000;">
    <option value="0" style="color:#0000FF;">Low</option>
    <option value="1" selected="selected" style="color:#008000;">Normal</option>
    <option value="2" style="color:#B22222;">Critical</option>
</select>

现在有了那个选择框,我想创建一个按钮单击时会将选择框的选定项目返回到 <option>selected="selected" .现在可以用简单的 jQuery 代码实现吗?

最佳答案

如果我很清楚就没有必要使用 jQuery:只需将选择包装在一个表单中并使用重置按钮(参见 http://jsfiddle.net/vwqVJ/5/)

<form ...>
    <select id="txtPriority" style="color:#008000;">
    ...
    </select>

    <button type="reset">Restore choice</button>
</form>

这样当你改变select时你总是可以回到初始状态

如果您需要改为使用 jQuery,只需在表单元素上调用 reset 方法,例如

$('form').get(0).reset()

否则,如果你根本没有表格(但你确实应该使用它)就使用

$('#txtPriority').val($('#txtPriority option[selected]').val())

关于jquery - 使用 jQuery 将选择框设置为其默认选定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10511879/

相关文章:

javascript - json 作为对象而不是字符串出现

jQuery:获取样式属性并将其附加到新的 <div>

javascript - 将日期作为查询字符串参数传递给共享点 ListView Web 部件

javascript - 禁用页面上除chrome扩展的后台javascript之外的所有脚本

html - Bootstrap 堆叠行中的间隙

html - 列表中的图标不适合左边距?有一个空间

javascript - 如何使用 localstorage 将列表中的所有复选框 bool 值存储到数组中?

javascript - 相当于动态创建 id 的 jQuery

php - 您在子页面上的 sql 查询如何确定使用哪个菜单导航到该页面?

jQuery 显示 DIV 并滚动到它,网址中没有主题标签