2012-03-13 70 views
-1

如何根据选定的值对jquery multiselect进行排序???我想先在jquery multiselect中显示选定的选项,然后是所有未选中的值。jquery Multiselect

like below multiselect having two values selected 

    <select id="the-select"> 
     <option value="1">First option</option> \\selected 
     <option value="2">Second option</option> 
    <option value="3">Third option</option>\\selected 
    </select> 


    but i need to show the multiselect as its showing below 

    <select id="the-select"> 
     <option value="1">First option</option> \\selected 
    <option value="3">Third option</option>\\selected 
    <option value="2">Second option</option> 
    </select> 

    Thanks 
+0

注意,当你分析你的网页在第一时间这可以工作,但请小心'即时'分类。对于用户来说,当选项的顺序每次选择时都会发生变化,这可能会让人感到困惑。考虑以直观的方式排列你的选项,例如字母。 – giorgio 2012-03-13 13:07:06

+0

谢谢答复。我只需要在加载时进行排序....我的意思是当页面加载第一次。如果您可以与我分享示例代码,将不胜感激 – Gautam 2012-03-13 13:15:25

回答

1

这是3线的解决方案:

var selected = $("select[name=yourselectname] option:selected"); //get every selected option 
selected.remove(); //remove them from the select 
$("select[name=yourselectname]").prepend(selected); //add them at the beginning of the select 

测试,:-) approuved

+0

感谢您的回复,但我认为你没有得到我的问题。我想先显示所有选中的值,然后再选择所有未选中的值。 – Gautam 2012-03-13 13:26:25

+0

这就是它所做的:-)。尝试一下 ! – Alytrem 2012-03-13 13:28:11

+0

是的,我尝试了相同的,但没有工作:(这是我的代码$(“#positionId”)。multiselect(); //评论开始:id和名称是相同的,这是= positionId评论结束// var selected:$(“select [name = positionId] option:selected”); selected.remove(); $(“select [name = positionId]”)。prepend(selected); – Gautam 2012-03-13 13:29:59