简体   繁体   English

如何使用jQuery使文本框可拖动?

[英]How to make text box draggable using jQuery?

Is anybody there who has experience with making html input text box draggable using jQuery? 有没有人有使用jQuery制作html输入文本框的经验?

I've tried to wrap text box inside div and can make it resizable, but not draggable. 我试图在div中包装文本框,可以使其可调整大小,但不能拖动。 The div and text box are placed on standard jQuery UI dialog. div和文本框放在标准的jQuery UI对话框中。 Actually, I need both - draggable and resizable html input text box inside dialog. 实际上,我需要两个 - 对话框内的可拖动和可调整大小的html输入文本框。

The code is as follows: 代码如下:

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $("#btnShow").click(function(e) {
            $('#dialog').dialog("open");
        });
        $('#dialog').dialog({
            title: "Sample dialog",
            resizable: false,
            autoOpen: false,
            width: 400,
            height: 300,
            buttons: [{ text: "OK", click: function() { /* do something */ } },
                { text: "Cancel", click: function() { $(this).dialog("close") } }]
        });
        $('#divText1').draggable({
            containment: "parent",
            cursor: "move"
        }).resizable({
            containment: "parent",
            handles: "e, w"
        });
    });
</script>

    <input id="btnShow" type="button" value="Show" />

    <div id="dialog" title="Dialog Box" style="border: solid 1px black; margin: 0px 0px 0px 0px; padding: 5px 0px 0px 5x;">
        <div id="divText1" style="width: 200px; height: 30px;">
            <input type="text" style="width: 98%;" value="Hello world!" /><br /><br />
        </div>
    </div>

Thank you in advance. 先感谢您。

Goran 戈兰

Similar example: 类似的例子:

 <script type="text/javascript">
            $(function () {
                $(".draggable").draggable({
                    revert: true,
                    helper: 'clone',
                    start: function (event, ui) {
                        $(this).fadeTo('fast', 0.5);
                    },
                    stop: function (event, ui) {
                        $(this).fadeTo(0, 1);
                    }
                });

                $("#droppable").droppable({
                    hoverClass: 'active',
                    drop: function (event, ui) {
                        this.value = $(ui.draggable).text();
                    }
                });
            });
        </script>

By default dragging is disabled on elements of type input,textarea,button,select,option . 默认情况下,对input,textarea,button,select,option类型的元素禁用拖动。 You can change/clear this using the cancel option. 您可以使用cancel选项更改/清除此项。

Working code: 工作代码:

 $("#btnShow").click(function(e) { $('#dialog').dialog("open"); }); $('#dialog').dialog({ title: "Sample dialog", resizable: false, autoOpen: false, width: 400, height: 300, buttons: [{ text: "OK", click: function() { /* do something */ } }, { text: "Cancel", click: function() { $(this).dialog("close") } } ] }); $('#divText1').draggable({ cancel: null, containment: "parent", cursor: "move" }).resizable({ containment: "parent", handles: "e, w" }); 
 <link href="https://code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input id="btnShow" type="button" value="Show" /> <div id="dialog" title="Dialog Box" style="border: solid 1px black; margin: 0px 0px 0px 0px; padding: 5px 0px 0px 5x;"> <div id="divText1" style="width: 200px; height: 30px;"> <input type="text" style="width: 98%;" value="Hello world!" /><br /><br /> </div> </div> 

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

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