[英]How can I detect if text is paste using right-click
If I paste text using ctrl c and ctrl v on table1(For example on "Apple"), duplicate text on input on table2 still change, but If i paste using right click and paste,duplicate input on table 2 does NOT change. 如果我在table1上使用ctrl c和ctrl v粘贴文本(例如在“Apple”上),则table2上输入的重复文本仍然会更改,但如果我使用右键单击并粘贴,则表2上的重复输入不会更改。 :( Ive created two different event(keyup and change) but nothing seems to work when text is paste using right click. Please see below : :(我已经创建了两个不同的事件(键盘和更改),但是当使用右键单击粘贴文本时似乎没有任何工作。请参阅下面:
Keypress fiddle demo Keypress小提琴演示
$(document).off('keydown').on('keydown', $('#table1 tbody tr td input:eq(0)'), function (e) {
var oldValue =$(e.target).val();//get the input value before keypress or edit
$(document).on('keyup', $('#table1 tbody tr td input'),function(e){
$('#table2').find('td input').each(function(){
if($(this).val() === oldValue){
$(this).val($(e.target).val());
}
$(document).off('keyup');
});
});
});
var oldValue;
$(document).on('keydown', '.main', function (e) {
oldValue = $(this).val();
foo(oldValue);
});
var newValue;
$(document).on('keyup', '.main', function (e) {
newValue = $(this).val();
foo(oldValue);
});
function foo(oldValue) {
$('#table1').find('tbody tr').find('td input').change(function (i) {
var orig = $(this).val();
$('#table2 tbody tr').find('td input').each(function (i) {
if (oldValue == $(this).val()) {
$(this).val(orig);
}
});
});
}
You can count the characters onChange (since you can only enter one character at a time. 您可以计算字符onChange(因为您一次只能输入一个字符)。
Edit: 编辑:
Why it wasn't working: 为什么不工作:
on your jsfiddle remember to set onDomReady
in the frameworks & extension for the equivalent of $(document).ready(handlerFn)
在你的jsfiddle上记得在框架和扩展中设置onDomReady
,相当于$(document).ready(handlerFn)
When you use on('change', handlerFn)
or .change(handlerFn)
on an input it will fire only after the textbox loses focus ( blur
). 当您on('change', handlerFn)
使用on('change', handlerFn)
或.change(handlerFn)
时,它将仅在文本框失去焦点( blur
)后才会触发。 The response is not instantaneous like when you use select
on your forms. 当您在表单上使用select
时,响应不是即时的。 Use bind("input", handlerFn)
instead of on(change)
for inputs. 对bind("input", handlerFn)
使用bind("input", handlerFn)
而不是on(change)
。
The code below will update the matching word on #table2 from the one being edited on #table1. 下面的代码将更新#table2上正在编辑的#table1上的匹配单词。 Updating will work for copy-paste CTRL C/V or on mouse copy/paste events. 更新将适用于复制粘贴CTRL C / V或鼠标复制/粘贴事件。 It will also alert if the user copy/paste by comparing the length of the old and new value. 如果用户通过比较旧值和新值的长度来复制/粘贴,它也会发出警报。
$("#table1 >* input").each(function() {
var elem = $(this),
oldValue;
elem.on('focus', function () {
elem.data('oldVal', elem.val());
elem.data('oldLen', elem.data('oldVal').length);
});
// Look for changes in the value,
// bind 'input' event to the textbox to fire the function
// every time the input changes (paste, delete, type etc.)
elem.bind("input", function(event){
oldValue = elem.data('oldVal');
// update oldVal
elem.data('oldVal', elem.val());
// check if pasted
if (elem.val().length - elem.data('oldLen') > 1 ) {
alert('Most certainly pasted');
}
// update input value length
elem.data('oldLen', elem.data('oldVal').length);
// update #table2
foo(oldValue, elem.val()) ;
});
});
And the function to update #table2 以及更新#table2的功能
function foo(oldValue, newValue) {
$('#table2')
.find('input')
.each(function (i) {
if (oldValue === $(this).val()) {
$(this).val(newValue);
}
});
}
Check this code, Hope this will help you: 检查此代码,希望这会对您有所帮助:
jQuery('#some_text_box').on('input propertychange paste', function() {
var text1 = $('#some_text_box').val();
//alert(text1);
$('#tab2').val(text1);
});
This is your first text box #some_text_box
. 这是你的第一个文本框#some_text_box
。
<input type='text' value = "Apple" id='some_text_box' />
And this is table 2 text box #tab2
这是表2的文本框#tab2
<input type='text' value="Apple" id='tab2'/>
Ok well this will detect for you if the user uses Ctrl + V etc but thats it. 好吧,如果用户使用Ctrl + V等,这将检测到你,但就是这样。 If they right click and paste then you will need another expression to capture that. 如果他们右键单击并粘贴,那么您将需要另一个表达式来捕获它。 I left this open by using keydown
and keyup
so you can capture more variations if needed. 我通过使用keydown
和keyup
保持打开状态,以便您可以根据需要捕获更多变体。
This is jQuery so you will need the library to cover this. 这是jQuery所以你需要库来覆盖这个。
$(document).ready(function()
{
var ctrlDown = false;
var ctrlKey = 17, vKey = 86, cKey = 67;
$(document).keydown(function(e)
{
if (e.keyCode == ctrlKey) ctrlDown = true;
}).keyup(function(e)
{
if (e.keyCode == ctrlKey) ctrlDown = false;
});
$("#no-copy-paste").keydown(function(e)
{
if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) return false;
});
});
Here is an article on handlers that may be some help to you as well. 这是一篇关于处理程序的文章,也可能对你有所帮助。 http://unixpapa.com/js/key.html http://unixpapa.com/js/key.html
-Epik -Epik
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.