设置一个使用jQuery显示在第一个位置的选项 [英] Set an option to be displayed at first position using jQuery

查看:100
本文介绍了设置一个使用jQuery显示在第一个位置的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有某些选项的select元素:

I have a select element with some options:

<select id="choose">
    <option id="first" value='null'>choose</option>
    <option id="g1" value='1'>gate1</option>
    <option id="g2" value='2'>gate2</option>
</select> 

用户选择一个选项并提交表单.数据已存储. 每当用户回来时,我都希望显示以前保存的选项 在选择元素的顶部,而不是选择.

The user selects an option and submits the form. The data is stored. Anytime the user comes back I want the previously saved option to be display at the top of the select element, instead of choose.

我这样做,例如这样

$("#choose").val("2");

它只是突出显示列表中的选项,但是select仍然是第一个元素.我该怎么做?

It just highlights the option in the list, but choose is still the first element. How do I accomplish that?

推荐答案

听起来像您想要在选定项目后将其移至顶部的声音.

Sounds like you want to move the selected item to the top after they select it.

只需绑定到更改事件并找到所选选项,然后将其添加到#choose

Just bind to the change event and the find the selected option and prepend it to the #choose

var choose = $("#choose").bind('change',function(){
    choose.find('option:selected').prependTo(choose);
});

http://jsfiddle.net/petersendidit/ZuhqS/1/

这篇关于设置一个使用jQuery显示在第一个位置的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆