如何在用户拖放时使用JQuery检测值更改事件将文本放入文本框?

时间:2011-09-01 00:56:01

标签: jquery input drag-and-drop

我正在使用这个jQuery代码

$('input').bind('change mouseup', ... )

检测用户是否将文本拖到我的输入和放大器中改变它的价值。但这似乎不起作用。为什么它不起作用以及如何让它起作用?

3 个答案:

答案 0 :(得分:7)

var inputField = $("input");
var oldValue = inputField.text();
inputField.bind("drop", function() {
    //when the drop happens the input value will not be updated yet
    //use a timeout to allow the input value to change.
    setTimeout($.proxy(function() {
        if (this.value !== oldValue) {
            $(this).trigger('change');
        }
    }, this), 0);
});
$("input").bind("change", function() {
    oldValue = this.value;
});

运行代码: http://jsfiddle.net/paulwesterberg/FJuvz/5/

答案 1 :(得分:2)

jQuery 1.10.1 - 我在这方面取得了成功...

$('body').on('input paste drop', '#txt-some-id, function() {
    $('#btn-some-id').prop("disabled", $(this).val().length === 0);
});

答案 2 :(得分:1)

如果您想要使用它或触发更改事件,则可以使用jQuery paste事件,并且只有少量超时才能粘贴该值。试试这个

$("input").bind('paste', function(e) {
        var $el = $(this);
        setTimeout(function() {
            //Here you can use the pasted value or trigger the text change event
            $el.trigger('change');
        }, 100);
});