简体   繁体   English

将复选框的选择从一个div移动到另一个

[英]Moving selections of checkboxes from one div to another

I've simplified as much as I can, but might still be best to take a look at JSfiddle first. 我已尽力简化了,但最好还是先看看JSfiddle。 https://jsfiddle.net/27a8qypq/3/ I start by ticking the checkbox'European countries' on the BOTTOM box, so that Austria and Spain appear. https://jsfiddle.net/27a8qypq/3/我首先在BOTTOM框上勾选“欧洲国家”复选框,以便出现奥地利和西班牙。 Now, if I tick 'Europe ONLY' on the TOP box what I want is for 'Austria' and 'Spain' labels & checkboxes to move from the BOTTOM up to the TOP box. 现在,如果我在“顶部”框上勾选“仅欧洲”,我想要的是“奥地利”和“西班牙”标签和复选框从底部移到顶部框。 Note that if the African countries checkbox had also been ticked, then 'Egypt' and 'Nigeria' must of course remain displayed in the bottom box. 请注意,如果还选中了非洲国家复选框,则“ Egypt”和“ Nigeria”当然必须保留在底部的框中​​。 I also need countries to always appear in alphabetical order, whichever box they are in. 我还需要国家/地区始终以字母顺序显示,无论它们位于哪个框。

After many failed hours on this my noobie brain's gone dead. 经过许多小时的失败之后,我的noobie大脑已经死了。 Can anyone help? 有人可以帮忙吗?

//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
<label>
    <input type="checkbox" name="colorCheckbox" id="EuropeONLY" />
    Europe ONLY
</label>

<div class="myTopBox">
    <label id="TextID" class="TextinTopBox">My European countries are...</label>
</div>
<br>
<label>
    <input type="checkbox" name="colorCheckbox" id="Europe" />
    European countries
</label>
<label>
    <input type="checkbox" name="colorCheckbox" id="Africa" />
    African countries
</label>

<div class="myBottomBox">
    <label class="myEurope">
        <input type="checkbox" value="Spain"/>
        Spain
    </label>
    <label class="myEurope">
        <input type="checkbox" value="Austria"/>
        Austria
    </label>
    <label class="myAfrica">
        <input type="checkbox" value="Nigeria"/>
        Nigeria
    </label>
    <label class="myAfrica">
        <input type="checkbox" value="Egypt"/>
        Egypt
    </label>
</div>

$(function() {
    $('input[type="checkbox"]').click(function() {
        if ($(this).attr("id") == "EuropeONLY") {
            $(".TextinTopBox").slideToggle(200)
        }
    });
});

function sortByText(a, b) {
    return $.trim($(a).text()) > $.trim($(b).text());
}

var li = $(".myBottomBox").children("label").detach().sort(sortByText)
$(".myBottomBox").append(li)

$('input[type="checkbox"]').click(function() {
    $('.my' + $(this).attr("id")).slideToggle(200)
});

.myTopBox {
    height: 100px;
    width: 300px;
    border: 1px solid green;
}

.TextinTopBox {
    display: none;
}

.myBottomBox {
    border: 1px solid blue;
    height: 100px;
    width: 300px;
    border: 1px solid blue;
}

.myEurope {
    display: none;
}
.myAfrica {
    display: none;
}

You can use appendTo to cut and paste a DOM element to another location. 您可以使用appendTo剪切DOM元素并将其粘贴到另一个位置。 See the fiddle that shows how that works for your case. 请参阅小提琴 ,该小提琴显示了如何针对您的情况工作。

$('input[type="checkbox"]').click(function() {
  if ($(this).attr("id") == "EuropeONLY") {
    var targetBox = $(this).prop("checked") ? '.myTopBox' : '.myBottomBox'
    $('.myEurope').appendTo($(targetBox))
  }
});

What you'll still need to take care of is the sorting, but i hope the fiddle is enough to get you started. 您仍然需要照顾的是排序,但是我希望小提琴足以使您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM