简体   繁体   English

如何使用右键单击检测文本是否粘贴

[英]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');
        });
    });              
});

on change fiddle demo 改变小提琴演示

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

here's a jsfiddle for you to play with 这是一个让你玩的小提琴

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'/>

JSFiddle 的jsfiddle

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. 我通过使用keydownkeyup保持打开状态,以便您可以根据需要捕获更多变体。

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.

相关问题 如何从右键单击上下文菜单中捕获粘贴事件? - How to capture paste event from right-click context menu? 如何使用 JavaScript 检测鼠标右键单击 + 粘贴? - How to detect right mouse click + paste using JavaScript? 为什么我们不能在Firefox中使用粘贴插件右键单击粘贴到TinyMCE中? - Why can't we right-click paste into TinyMCE with paste plugin in Firefox? 为了选择或聚焦对象,如何使右键单击的行为与单击鼠标左键的行为相同 - How can I make a right-click behave as a left-click for the purpose of selecting or focusing an object 检测右键单击 Ace 编辑器 - Detect a right-click on Ace editor 如何在node.js selenium-webdriver中模拟右键单击? - How can I emulate right-click in the node.js selenium-webdriver? 无法使用 JavaScript 禁用打印屏幕、复制/粘贴和右键单击功能 - Trouble disabling the Print Screen, Copy/Paste and right-click functionalities using JavaScript 在右键单击剪切/粘贴时更新表单验证 - Update form validation on right-click cut/paste 使用 Ctrl+v 或右键单击 -&gt; 粘贴检测粘贴的文本 - Detect pasted text with Ctrl+v or right click -> paste Flot-检测左键单击和右键单击 - Flot - Detect left-click and right-click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM