gpt4 book ai didi

javascript - 带条件的 JQuery 选择框循环

转载 作者:行者123 更新时间:2023-11-30 06:37:51 25 4
gpt4 key购买 nike

我尝试使用 JQuery Select Box and Loop Help 问题中的内容,但我的实现似乎有点不同,我似乎无法使其工作。

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

我做到了,所以我不能从列表底部移动选项 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'));
}
}
})
});

最佳答案

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

   $('#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');
}
}
});
});

关于javascript - 带条件的 JQuery 选择框循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13420194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com