开发者

Move an item up/down a select

开发者 https://www.devze.com 2022-12-22 18:08 出处:网络
I\'m wondering if it\'s possible to change the order of things in a select control without having to totally rebuild it?

I'm wondering if it's possible to change the order of things in a select control without having to totally rebuild it?

Is there a javascript function where I can cha开发者_如何学运维nge the "index" of a specific option in the select?

Thanks


Sure, just find the two elements in jQuery (by their IDs or whatever), so you have two objects and then use before() on them

var o1=$("#opt1");
var o2=$("#opt2");
o2.insertBefore(o1);


Try using Array.splice

// Remove the option from the list:
var option = selectElement.options.splice(indexOfOptionToRemove,1);

// and put it back in at the new index:
selectElement.options.splice(indexOfNewOptionPosition,option);
0

精彩评论

暂无评论...
验证码 换一张
取 消