繁体   English   中英

如何比较jQuery的可拖动元素?

[英]How do I compare draggable element by jQuery?

我的html代码是

<div id="products">
    <div class="ui-widget-content">
        <ul>
            <li data-id="1" class = "credit"> 10000$ </li>
            <li data-id="2"class = "credit"> 5000$ </li>
            <li data-id="3" class = "credit"> 10000$ </li>
            <li data-id="4" class = "credit"> 5000$ </li>
        </ul>
    </div>
</div>
<table width:100%>
    <tr><td>
 <h3 class="ui-widget-header">Yahoo</h3>
<table border=1 width=100%>
    <tr>
        <td><div id="shoppingCart1" class="shoppingCart">
    <h7 class="ui-widget-header">Number 1</h7>
    <div class="ui-widget-content">
        <ol>
            <li class="placeholder">Add your items here</li>
        </ol>
    </div>
</div>
     </td>

    </tr>
</table>
        </td><td>
 <h3 class="ui-widget-header">Yahoo</h3>
<table border=1 width=100%>
    <tr>
        <td><div id="shoppingCart1" class="shoppingCart">
    <h7 class="ui-widget-header">Number 2</h7>
    <div class="ui-widget-content">
        <ol>
            <li class="placeholder">Add your items here</li>
        </ol>
    </div>
</div>
     </td>
    </tr>
</table>
        </td></tr></table>

而我的js代码是

$("#products li").draggable({
    appendTo: "body",
    helper: "clone"
});
$("#shoppingCart1 ol").droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    accept:".credit",
    drop: function(event, ui) {
        var self = $(this);
        self.find(".placeholder").remove();
        var productid = ui.draggable.attr("data-id");
        if (self.find("[data-id=" + productid + "]").length) return;
        $("<li></li>", {
            "text": ui.draggable.text(),
            "data-id": productid
        }).appendTo(this);
        // To remove item from other shopping chart do this
        var cartid = self.closest('.shoppingCart').attr('id');
        $(".shoppingCart:not(#"+cartid+") [data-id="+productid+"]").remove();
    }
}).sortable({
    items: "li:not(.placeholder)",
    sort: function() {
        $(this).removeClass("ui-state-default");
    }
});
$("#shoppingCart2 ol").droppable({
    activeClass: "ui-state-default",
    hoverClass: "ui-state-hover",
    accept:".debit",
    drop: function(event, ui) {
        var self = $(this);
        self.find(".placeholder").remove();
        var productid = ui.draggable.attr("data-id");
        if (self.find("[data-id=" + productid + "]").length) return;
        $("<li></li>", {
            "text": ui.draggable.text(),
            "data-id": productid
        }).appendTo(this);
        // To remove item from other shopping chart do this
        var cartid = self.closest('.shoppingCart').attr('id');
        $(".shoppingCart:not(#"+cartid+") [data-id="+productid+"]").remove();
    }
}).sortable({
    items: "li:not(.placeholder)",
    sort: function() {
        $(this).removeClass("ui-state-default");
    }
});

现在您也可以在此处查看演示-http: //jsfiddle.net/Sanjayrathod/5DMru/

我想要的是,如果我将一个项目拖放到“数字1”框中,然后再将一个项目拖放到“数字2”框中,现在在将这些项目拖放到相应的“数字”框中之后,我想获得的总/求和值分别比较各个框,然后比较各个框的总和,即比较数字框1的总和与数字框2的总和

您只需要遍历两个框中的每个li并对它们的值求和:

var amount1 = 0;
$("#shoppingCart1 ol").find('li').each(function () {
    amount1 += parseInt($(this).text());
});
var amount2 = 0;
$("#shoppingCart2 ol").find('li').each(function () {
    amount2 += parseInt($(this).text());
});
if(amount1 > amount2) { // the value in the first box is greater than in the second
    //TODO notify user
} else if (amount1 < amount2) { // the value in the first box is less than in the second
    //TODO notify user
} else { // // the value in the first box is equal to the second
    //TODO notify user
}

添加值后,在每个可放置的句柄中执行此操作。 我更新了您的小提琴: 请参阅此处

顺便说一句,您的代码中有一个错别字:两个盒子的ID为shoppingCart1而不是第二个盒子的ID为shoppingCart2

暂无
暂无

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

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