Dragging and dropping a <li> id to a text input

两盒软妹~` 提交于 2020-01-15 12:19:07

问题


I've searched the interwebs, and I'm not working out how to do that. I would like to have something like a file list, and the drag and drop a file (a <li> element) and write it's id to a text field. Would it be possible ?


回答1:


Yes it is possible using jQuery UI draggable and droppable plugins. You need to include jQuery UI script reference into your page.

jQuery UI CDN - http://code.jquery.com/ui/1.11.4/jquery-ui.min.js

Html

<ul>
    <li id="li1">Li 1</li>
    <li id="li2">Li 2</li>
    <li id="li3">Li 3</li>
</ul>
<br />
<br />
<input type="text" id="textbox" />

JS

$('ul li').draggable({
    revert: true,
    helper: 'clone'
});

$("#textbox").droppable({
    drop: function (event, ui) {
        this.value = $(ui.draggable).attr('id');
    }
});

Demo http://jsfiddle.net/bxjmtdkv/2/



来源:https://stackoverflow.com/questions/30669577/dragging-and-dropping-a-li-id-to-a-text-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!