【问题标题】:Jquery Multiple Draggable and Droppable ItemsJquery 多个可拖放项目
【发布时间】:2015-09-09 12:08:20
【问题描述】:

我有一个 jquery droppable 购物车,我通过 Sarath Sprakash 从这段代码中定制了它。它允许捐赠者从多个预定义的捐赠金额中进行选择,并将它们拖到他们希望支持的类别中,并将它们添加到总数中。

一切正常,除非您将可拖动对象拖出可放置对象。如果您在不同的 droppables 中有两次或更多 10 英镑的捐款并删除了一个,它也会删除另一个,尽管总数加起来是正确的。

我怀疑这与克隆有关,但无法追踪问题。有人可以帮忙吗?

小提琴http://jsfiddle.net/mrdh592z/

HTML:

<div id="dragAndDrop"><a id="dragtop"></a>&nbsp;
<div id="amounts">
<h2>Please place stickers for the amount you want to give in any or all of these circles to help people move out of homelessness.</h2>
<div class="ui-widget-content" data-value="10" data-count="0">&pound;10</div>
<div class="ui-widget-content" data-value="15" data-count="0">&pound;15</div>
<div class="ui-widget-content" data-value="25" data-count="0">&pound;25</div>
<div class="ui-widget-content" data-value="50" data-count="0">&pound;50</div>
<div class="ui-widget-content" data-value="100" data-count="0">&pound;100</div>
</div>
<div id="categories">&nbsp;</div>
<div id="descriptionWrap">
<div class="description">
<div id="first" class="droppable" data-count="0">&nbsp;</div>
<h3>First Meeting</h3>
<p>Help us be there for more homeless people with a friendly smile, a warming cup of tea and precious time to talk.</p>
</div>
<div class="description">
<div class="droppable" data-count="0">&nbsp;</div>
<h3>Goal Setting</h3>
<p>Give the support many homeless people need to identify positive goals and work towards them, one step at a time.</p>
</div>
<div class="description">
<div class="droppable" data-count="0">&nbsp;</div>
<h3>IT Skills</h3>
<p>Help us provide more classes so that people can learn IT skills to help them build their confidence and find a job.</p>
</div>
<div class="description">
<div class="droppable" data-count="0">&nbsp;</div>
<h3>CV Skills</h3>
<p>Help give more homeless people the training, support and encouragement they need to create good CVs and find employment.</p>
</div>
<div class="description">
<div class="droppable" data-count="0">&nbsp;</div>
<h3>A home of their own</h3>
<p>Provide advice, support and household items to people who are setting up home again after months or years of being homeless.</p>
</div>
</div>
<div id="totalWrap">
<div id="totalText">Total : &pound;</div>
<div id="counter">0</div>
<div id="reset"><input id="zero" type="button" value="Reset" /></div>
</div>
</div>

jQuery

var a = 0;
    var i = 0;
var q = 0;
    var drg;
var z = parseInt($('.droppable').attr('data-count'));

$(function () {
    var cln;
    $(".ui-widget-content").draggable({
        drag: function (event, ui) {
           drg = $(this);
            $(".droppable").droppable('enable');
        },
        helper: 'clone'

    });

    $(".droppable").droppable({
tolerance: "fit",
        drop: function () {
var q = parseInt($('.droppable').children('.runningTotal').html());
            a = a + parseInt(drg.attr('data-value'));
            $("#counter").html(a);
             i++;
             z++;
q=q+parseInt($(drg).attr('data-value'));
           var txt = drg.text() + "1";
                $(this).append("<div class='mini' id=" + drg.text() + " data-value=" + drg.attr('data-value') + ">" + drg.text() + "</div>");
                 q==0;


            $("#" + drg.text()).draggable({
                drag: function (event, ui) {
                    drg = $(this);


                },
                stop: function (event, ui) {
                    var tt = drg.text() + "1";
                     i--;
                    if (parseInt($("#" + tt).text()) > 1) {
                        $("#" + tt).html(parseInt($("#" + tt).text()) - 1);



                    } else {

                        $("#" + drg.text()).remove();

                        $(this).remove();

                    }
                    a = a - parseInt(drg.attr('data-value'));
                    $("#counter").html(a);

                },
                helper: 'clone'
            });
        }

    });
});
$('#zero').click(function() {
$('#PC14234_txtAmount').val(0);
$('.mini').remove();
$('#counter').html('0');
});

【问题讨论】:

    标签: jquery jquery-ui draggable cart droppable


    【解决方案1】:

    所有相同数量都被删除的原因是它的 ID。对于所有相同的数量,它都是相同的,因此 .remove() 会从 droppable 中删除所有克隆。所以要摆脱这个问题,你必须让 id 独一无二。将您的 JS 代码替换为以下 JS 代码:

    var a = 0;
    var i = 0;
    var q = 0;
    var drg;
    var z = parseInt($('.droppable').attr('data-count'));
    var cnt=0;//counter variable for unique id
    
    $(function () {
      var cln;
      $(".ui-widget-content").draggable({
        drag: function (event, ui) {
           drg = $(this);
            $(".droppable").droppable('enable');
        },
        helper: 'clone'
     });
    
    $(".droppable").droppable({
        tolerance: "fit",
        drop: function () {
        var q = parseInt($('.droppable').children('.runningTotal').html());
            a = a + parseInt(drg.attr('data-value'));
            $("#counter").html(a);
             i++;
             z++;
        q=q+parseInt($(drg).attr('data-value'));
           var txt = drg.text() + "1";
            cnt=cnt+1;
            var id=cnt+parseInt($(drg).attr('data-value'));//creating unique id by adding cnt value to it.
                $(this).append("<div class='mini' id=" +id + " data-value="     + drg.attr('data-value') + ">" + drg.text() + "</div>");
                 q==0;
    
            $("#" + id).draggable({
                drag: function (event, ui) {
                    drg = $(this);
                },
                stop: function (event, ui) {
                    var tt = id + "1";
                     i--;
                    if (parseInt($("#" + tt).text()) > 1) {
                        $("#" + tt).html(parseInt($("#" + tt).text()) - 1);
    
                    } else {
                        $("#" + id).remove();
                        $(this).remove();
                    }
                    a = a - parseInt(drg.attr('data-value'));
                    $("#counter").html(a);
                },
                helper: 'clone'
            });
        }
      });
    });
    $('#zero').click(function() {
    $('#PC14234_txtAmount').val(0);
    $('.mini').remove();
    $('#counter').html('0');
    });
    

    【讨论】:

    • 非常感谢@Akshay。这非常有效。您能否突出显示您已编辑的行。是 $('#' + id).remove();
    • @PaulMcCarthy - 是的。我添加了一个名为“cnt”的新变量。在删除函数时,已在 cnt=cnt+1 中为这个“cnt”变量赋值; var id=cnt+parseInt($(drg).attr('data-value'));然后将此“id”变量用于可拖动功能,并在删除拖动元素时也使用。我在回答中也添加了评论,以供您在我进行任何更改时参考。
    猜你喜欢
    • 2014-05-29
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多