javascript - 用于对列表中的项目进行排序的旧 javascript

标签 javascript html

我一直使用此代码对列表中的项目进行排序,然后将订单提交回数据库。

但代码在 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/

相关文章:

javascript - php执行后的ajax回调

javascript - 过滤 Javascript/Lodash 中的嵌套属性

javascript - 禁用基于同一行上的另一个元素的选择

javascript - Angular JS - 根据其包含的数字进行过滤

javascript - 使用 jquery 显示格式化的原始 html

javascript - 获取2个元素之间的元素

javascript - jQuery get() 方法如何支持灵活的参数索引?

javascript - 在多级对象数组上使用过滤器?

javascript - 对象键在循环映射 react 渲染中未定义

javascript - Google Maps API 使用 JavaScript 获取位置和 ID