2011-09-07 64 views
1

我有一个jQuery可排序列表,其中可拖动列表元素拖入其中。JQuery可拖动+可排序 - 单击以添加

有什么方法通过单击它们(或单独的按钮)而不是拖动来添加元素到可排序列表中,并触发可排序的“接收”事件?

+0

你有没有使用jQuery UI http://jqueryui.com/demos/sortable尝试 –

+0

感谢您的信息娜迦,但我在努力寻找,帮助S中的文档中的任何olve我的问题。 – Brasso

回答

1

我不认为有一个内置的方式来触发(假设触发(“接收”)将diffucult得到工作),但这里是我的方式会去用它:

添加addToList函数调用您的可拖动列表项,然后创建此函数。如果您认为合适,自定义内部html/span标签。

function addToList(text,id){ 

var $list=$("ol#drop_zone"); 

item_html += "<span>"+text+"</span>"; 
var $listItem = $("<li/>",{ 
id   : id, 
html  : item_html 
}); 
$list.append($listItem); 
//bind a removeItem function to the new list item 
$listItem.find('span').bind('click',removeItem); 
//make a call to receive_item function that does the same as receive event 
receive_item(id); 
} 

作出了在你的丢弃事件和在手动addToList事件使用的接收项功能:

function receiveItem(id){ 
    $.ajax({ 
     type: 'post', 
     data: $('ol#drop_zone').sortable('serialize'), 
     dataType: 'script', 
     complete: function(request){ 
      alert('received!'); 
     }, 
     url: 'my_url'}) 

} 

试试这个清理重复的代码 - 编辑您的接收功能使用上述功能:

$('ol#drop_zone').sortable({ 
    receive: function(ev, ui){ 
    id=ui.item.attr('id');  
    receiveItem(id); 
    } 
}) 
相关问题