好的,我有一个选择框
<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>
Run Code Online (Sandbox Code Playgroud)
现在与该选择框,我想创建一个按钮,单击时将选择框的选择项返回到<option>有selected="selected".现在可以使用简单的jQuery代码吗?
如果我很好理解不需要使用jQuery:只需在表单中包含select并使用重置按钮(请参阅http://jsfiddle.net/vwqVJ/5/)
<form ...>
<select id="txtPriority" style="color:#008000;">
...
</select>
<button type="reset">Restore choice</button>
</form>
Run Code Online (Sandbox Code Playgroud)
这样,当您更改选择时,您始终可以返回初始状态
如果你需要使用jQuery,只需在表单元素上调用reset方法,比如
$('form').get(0).reset()
Run Code Online (Sandbox Code Playgroud)
否则,如果你根本没有形式(但你真的应该使用它),只需使用
$('#txtPriority').val($('#txtPriority option[selected]').val())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5998 次 |
| 最近记录: |