2013-05-09 182 views
2

我在將可拖動功能附加到動態創建的元素時遇到了嚴重的麻煩。當創建該項目時,我無法綁定droppable,因爲在某個元素被刪除時會發生ajax調用!我怎樣才能將可投入的功能綁定到動態元素上!如何將jquery droppable綁定到動態創建的元素

請回復!

可放開功能:

$('.bill-item-list').droppable({ 
        accept: '.item', 
        drop: function(event, ui) { 
         var clone = bill.clone(); 
         var droppable = $(this); 
         var jsonObject = 'itemId=' + ui.draggable.attr('id') + '&billId=' + $(this).parent().attr('id'); 
         $.ajax({ 
          url: "/addItemToBill", 
          type: "POST", 
          data: jsonObject, 
          dataType: "json", 
          beforeSend: function(x) { 
           if(x && x.overrideMimeType) { 
            x.overrideMimeType("application/json;charset=UTF-8"); 
           } 
          }, 
          success: function(result){ 
           if(result.status = true){ 
            clone.find('.bill-item-name-left').html(ui.draggable.find('.item-name').children('h4').html()); 
            clone.find('.bill-item-name-right').html(ui.draggable.find('.item-price').children('h4').html()); 
            clone.find('.bill-item-price').html(ui.draggable.find('.item-price').children('h4').html() + ".00 USD"); 
            clone.find('.bill-item-amount').html("1"); 
            if(droppable.height() > 300){ 
             droppable.css('overflow-y','scroll'); 
             droppable.css('height', '320px') 
            } 
            clone.draggable({ 
             revert : true, 
             zIndex: 1, 
             containment: "window", 
             opacity: 0.5, 
             cursor: "move", 
             helper: function() { return $(this).clone().appendTo('body').show(); } 
            }); 
            clone.insertBefore(droppable.find('.item-drop-point')).hide().fadeIn("slow"); 
           } 

          } 
         }); 
        } 
       }); 

和動態元素創建

function getBillInformation(Status,billingDate,User){ 
    var parent = ""; 
    var status = ""; 

    switch(Status){ 
     case "OPEN": 
      parent = $('#tab-1'); 
      status = "O"; 
      break; 
     case "SETTLED": 
      parent = $('#tab-2'); 
      status = "S"; 
      break; 
     case "CANCEL": 
      parent = $('#tab-3'); 
      status = "C"; 
      break; 
    } 

    var billList = parent.find('#bill-list').first(); 
    var bill = parent.find('.bill').first(); 
    var billItemList = parent.find('.bill-item-list').first(); 
    var billItem = parent.find('.bill-item').first(); 
    var billPanel = parent.find('#bill-panel').first(); 

    var jsonObject = "billStatus=" + status + "&billingDate=" + billingDate + "&user=" + User; 

    ajaxCall("/getBillingInformation","POST",jsonObject,function(response){ 
     billPanel.empty(); 

     billItemList.find('.bill-item').each(function(){ 
      $(this).remove(); 
     }) 

     for(var i = 0; i < response.bills.length; i++){ 
      var clone = bill.clone(); 
      clone.attr('id', response.bills[i].billId); 
      clone.find('.bill-number').html(response.bills[i].billNo); 
      clone.find('.amount').html(response.bills[i].billTotal + " USD"); 

      clone.find('.room-number').html(response.bills[i].roomNo); 
      clone.find('.table-name').html(response.bills[i].tableNo); 


      if(response.bills[i].itemsList.length != 0){ 
       for(var j = 0; j < response.bills[i].itemsList.length; j++){ 
        var billItemClone = billItem.clone(); 
        billItemClone.find('.bill-item-name-right').html(response.bills[i].itemsList[j].amount); 
        billItemClone.find('.bill-item-amount').html(response.bills[i].itemsList[j].qty); 
        var total = (response.bills[i].itemsList[j].amount) * (response.bills[i].itemsList[j].qty); 
        billItemClone.find('.bill-item-price').html(total + ".00 USD"); 
        billItemClone.draggable({ 
         revert : true, 
         zIndex: 1, 
         containment: "window", 
         opacity: 0.5, 
         cursor: "move", 
         helper: function() { return $(this).clone().appendTo('body').show(); } 
        }); 
        clone.find('.bill-item-list').prepend(billItemClone); 
       } 
      } 

      billPanel.prepend(clone); 
     } 
    }); 

} 
+0

顯示一些相關的代碼。 – 2013-05-09 06:10:26

+0

我用代碼更新了問題! – 2013-05-09 06:22:02

回答

5

一旦元素被添加到DOM,則需要對它們初始化投擲的小部件。

創建一個將轉成bill-item-list投擲的

function billItemDroppable(els){ 
    els.droppable({ 
     accept: '.item', 
     drop: function(event, ui) { 
      var clone = bill.clone(); 
      var droppable = $(this); 
      var jsonObject = 'itemId=' + ui.draggable.attr('id') + '&billId=' + $(this).parent().attr('id'); 
      $.ajax({ 
       url: "/addItemToBill", 
       type: "POST", 
       data: jsonObject, 
       dataType: "json", 
       beforeSend: function(x) { 
        if(x && x.overrideMimeType) { 
         x.overrideMimeType("application/json;charset=UTF-8"); 
        } 
       }, 
       success: function(result){ 
        if(result.status = true){ 
         clone.find('.bill-item-name-left').html(ui.draggable.find('.item-name').children('h4').html()); 
         clone.find('.bill-item-name-right').html(ui.draggable.find('.item-price').children('h4').html()); 
         clone.find('.bill-item-price').html(ui.draggable.find('.item-price').children('h4').html() + ".00 USD"); 
         clone.find('.bill-item-amount').html("1"); 
         if(droppable.height() > 300){ 
          droppable.css('overflow-y','scroll'); 
          droppable.css('height', '320px') 
         } 
         clone.draggable({ 
          revert : true, 
          zIndex: 1, 
          containment: "window", 
          opacity: 0.5, 
          cursor: "move", 
          helper: function() { return $(this).clone().appendTo('body').show(); } 
         }); 
         clone.insertBefore(droppable.find('.item-drop-point')).hide().fadeIn("slow"); 
        } 

       } 
      }); 
     } 
    }); 
} 

函數然後調用

billItemDroppable($('.bill-item-list')) 

然後,一旦動態項目創建

billPanel.prepend(clone); 
billItemDroppable(clone.find('.bill-item-list')) 
+0

我得到以下錯誤! HierarchyRequestError:節點無法插入層次結構中的指定點 – 2013-05-09 06:18:55

+0

@DimalChandrasiri請參閱updat – 2013-05-09 06:24:31

+0

謝謝!有用! – 2013-05-09 07:09:00

相關問題