将项目向上/向下移动选择项

时间:2022-10-09 14:29:29

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 change the "index" of a specific option in the select?

是否有一个javascript函数可以在select中更改特定选项的“索引”?

Thanks

谢谢

2 个解决方案

#1


5  

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

当然,只需在jQuery中找到这两个元素(通过它们的id或其他),这样就有了两个对象,然后在它们上面使用before()

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

#2


1  

Try using Array.splice

试着用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);

#1


5  

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

当然,只需在jQuery中找到这两个元素(通过它们的id或其他),这样就有了两个对象,然后在它们上面使用before()

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

#2


1  

Try using Array.splice

试着用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);