我一直使用此代码对列表中的项目进行排序,然后将订单提交回数据库。
但代码在 Firefox 或 Chrome 中不起作用。它跳转然后清除列表的内容。它从 2002 年开始使用,仅在 IE 中运行良好..
var list;
function moveUp() {
list = document.forms[0].lists;
var index = list.selectedIndex;
if (index > 0) {
var item = list.options[index];
list.remove(index);
list.add(item, index - 1);
}
}
function moveDown() {
list = document.forms[0].lists;
var index = list.selectedIndex;
if (index > -1 && index < list.options.length - 1) {
var item = list.options[index];
list.remove(index);
list.add(item, index + 1);
}
}
function doSubmit() {
var s = "";
list = document.forms[0].lists;
for (var i = 0; i < list.options.length; i++) {
s += list.options[i].value + " ";
}
document.forms[0].order.value = s;
return false;
}
最佳答案
问题出在 list.add(item,index)
上。这是非标准的,仅适用于 IE。对于 Chrome 和 Firefox,您应该使用 document.createElement("option")
创建一个 Option 元素。查看示例代码 here
关于javascript - 用于对列表中的项目进行排序的旧 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4014834/