2

我尝试使用 JQuery 选择框和循环帮助问题中的内容,但似乎我的实现有点不同,我似乎无法使其工作。

我有一个 fromSelectBox 和一个 toSelectBox ,我试图在其中移动选项。我试图设置两个条件。如果选项 A 放置在 toSelectBox 一侧,它应该始终位于底部。如果选项 B 已经在 toSelectBox 一侧,则不应允许移动选项 B。

我做到了,所以我不能从列表底部移动选项 A 或 B,也不能移动 A 或 B 下面的任何东西,但是当我一次移动一组项目时,订单可能会被打乱。

基本上我想要做的是任何时候一个项目从fromSelectBox移动到toSelect框,它应该循环并在必要时使用。

这是我到目前为止所拥有的。

function resortOnMove() {
    $('select').each(function(){
       $('option', this).each(function(){
        if ($('#toSelectBox option:selected').val() == '03' || $('#toSelectBox option:selected').val() == '01') {
           alert("moving now");
            if ($('#toSelectBox option:selected').index() < $('#toSelectBox option:last').index()) {
               $('#toSelectBox option:selected').insertAfter($('#toSelectBox option:last'));
          }
       }
       })
    });
4

1 回答 1

0

我决定与其重新排序,不如在添加倍数时在末尾附加值。

   $('#moveright').click(function () {
    var tempObjects = null;
    $('#fromSelectBox option:selected').each(function () {
        if (($('#toSelectBox option:last').val() == "03" || $('#toSelectBox option:last').val() == "01") && ($('#fromSelectBox option:selected').val() == "03" || $('#fromSelectBox option:selected').val() == "01")) {
            alert($('#toSelectBox option:last').text() + " and " + $('#fromSelectBox option:selected').text() + " cannot both be in Prioritized Categories.");
            return false
        }
        else if ($('#toSelectBox option:last').val() == "03" || $('#toSelectBox option:last').val() == "01") {
                var movingObjects = $(this);
                movingObjects.remove();
                movingObjects.insertBefore($('#toSelectBox option:last'));
        }
        else {
            $('#fromSelectBox option:selected').each(function () {
                if ($(this).val() === "01" || $(this).val() === "03") {
                    if (tempObjects!=null){
                        alert(tempObjects.text()+ " and " + $(this).text() + " cannot both be in Prioritized Categories.");
                        return false
                    }
                    tempObjects = $(this);
                    $(this).remove();
                }
                $(this).remove().appendTo('#toSelectBox');
            });
            if (tempObjects != null) {
                tempObjects.appendTo('#toSelectBox');
            }
        }
    });
});
于 2012-11-16T20:20:02.200 回答