How to use JQuery to detect value-changed event when user drag & drop text into textbox?

≯℡__Kan透↙ 提交于 2019-12-30 05:25:25

问题


I'm using this jQuery code

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

to detect if user drag text somewhere into my input & change it value. But that seems doesn't work. Why doesn't it work and how I can get it to work?


回答1:


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;
});

run the code: http://jsfiddle.net/paulwesterberg/FJuvz/5/




回答2:


jQuery 1.10.1 - I had success with this...

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



回答3:


You can use jQuery paste event with little amount of timeout until the value is pasted if you want to use it or fire a change event. Try this

$("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);
});


来源:https://stackoverflow.com/questions/7265119/how-to-use-jquery-to-detect-value-changed-event-when-user-drag-drop-text-into

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