Jquery / JS将“paste”事件处理程序绑定到输入文本框

时间:2021-04-09 20:27:23

Allright, SO i have an input box and I need to do things everytime it changes, I am having trouble doing it for mouse paste. Here is the code I have

好吧,所以我有一个输入框,我需要在每次更改时都做一些事情,我无法为鼠标粘贴做这件事。这是我的代码

$("#attack-navy"+unit.ID+"-number").bind('paste', function(){
            alert("paste detected");
            $("#attack-max-capacity").text(getMaxCapacity());
});

the getMaxCapacity() function return number entered * 30 for now;

getMaxCapacity()函数返回的数字现在为* 30;

Here is the scenario when
1: I paste 3, it will not change (i still see the alert)
2: Then when i paste 5, it will be 90(3 * 30)
3: Then if i paste 10 it will be 150(5 * 30), and so on.

这是1的情况:我粘贴3,它不会改变(我仍然看到警报)2:然后,当我粘贴5,它将是90(3 * 30)3:然后,如果我粘贴10将是150 (5 * 30),依此类推。

I think its doing the handler before the paste actually occurs. Any ideas on what I can do? (.change will not work, it must happen as soon as u paste)

我认为它在粘贴实际发生之前就做了处理程序。关于我能做什么的任何想法? (.change将无效,必须在粘贴后立即发生)

3 个解决方案

#1


45  

You should handle the input and propertychange events.
Demo.

您应该处理input和propertychange事件。演示。

#2


3  

You're right. The paste event is firing before the value of the input changes. Try wrapping your handler in a timeout:

你是对的。粘贴事件在输入值更改之前触发。尝试在超时中包装处理程序:

setTimeout(function() { $("#attack-max-capacity").text(getMaxCapacity()); }, 0);

#3


1  

Replace the bind event with .live and it should work, like this:

用.live替换绑定事件,它应该工作,如下所示:

$("#attack-navy"+unit.ID+"-number").live('paste', function(){
            alert("paste detected");
            $("#attack-max-capacity").text(getMaxCapacity());
});

#1


45  

You should handle the input and propertychange events.
Demo.

您应该处理input和propertychange事件。演示。

#2


3  

You're right. The paste event is firing before the value of the input changes. Try wrapping your handler in a timeout:

你是对的。粘贴事件在输入值更改之前触发。尝试在超时中包装处理程序:

setTimeout(function() { $("#attack-max-capacity").text(getMaxCapacity()); }, 0);

#3


1  

Replace the bind event with .live and it should work, like this:

用.live替换绑定事件,它应该工作,如下所示:

$("#attack-navy"+unit.ID+"-number").live('paste', function(){
            alert("paste detected");
            $("#attack-max-capacity").text(getMaxCapacity());
});