217

我正在寻找一种方法来清理我粘贴到浏览器中的输入,这可能与 jQuery 相关吗?

到目前为止,我已经设法想出了这个:

$(this).live(pasteEventName, function(e) {
 // this is where i would like to sanitize my input
 return false;
}

不幸的是,由于这个“小”问题,我的发展陷入了困境。如果有人能指出我正确的方向,我真的会让我成为一个快乐的露营者。

4

17 回答 17

347

好的,只是遇到了同样的问题..我走了很长一段路

$('input').on('paste', function () {
  var element = this;
  setTimeout(function () {
    var text = $(element).val();
    // do something with text
  }, 100);
});

在 .val() 函数可以被填充之前只是一个小的超时。

E.

于 2009-10-01T11:35:57.767 回答
77

您实际上可以直接从事件中获取价值。但是,如何到达它有点迟钝。

如果您不希望它通过,请返回 false。

$(this).on('paste', function(e) {

  var pasteData = e.originalEvent.clipboardData.getData('text')

});
于 2015-04-23T18:30:21.283 回答
44

为了跨平台兼容性,它应该处理 oninput 和 onpropertychange 事件:

$ (something).bind ("input propertychange", function (e) {
    // check for paste as in example above and
    // do something
})
于 2012-04-10T08:28:00.117 回答
19

我通过使用以下代码修复了它:

$("#editor").live('input paste',function(e){
    if(e.target.id == 'editor') {
        $('<textarea></textarea>').attr('id', 'paste').appendTo('#editMode');
        $("#paste").focus();
        setTimeout($(this).paste, 250);
    }
});

现在我只需要存储插入符号的位置并附加到那个位置然后我就准备好了......我想:)

于 2009-03-27T05:47:19.120 回答
10

嗯...我认为您可以使用它e.clipboardData来捕获正在粘贴的数据。如果没有成功,请看这里

$(this).live("paste", function(e) {
    alert(e.clipboardData); // [object Clipboard]
});
于 2009-03-26T18:37:45.047 回答
9

监听粘贴事件并设置一个 keyup 事件监听器。在 keyup 上,捕获值并删除 keyup 事件侦听器。

$('.inputTextArea').bind('paste', function (e){
    $(e.target).keyup(getInput);
});
function getInput(e){
    var inputText = $(e.target).val();
    $(e.target).unbind('keyup');
}
于 2010-08-26T16:18:35.317 回答
7
$("#textboxid").on('input propertychange', function () {
    //perform operation
        });

它会正常工作。

于 2017-02-21T12:55:05.940 回答
6

这越来越接近您可能想要的。

function sanitize(s) {
  return s.replace(/\bfoo\b/g, "~"); 
};

$(function() {
 $(":text, textarea").bind("input paste", function(e) {
   try {
     clipboardData.setData("text",
       sanitize(clipboardData.getData("text"))
     );
   } catch (e) {
     $(this).val( sanitize( $(this).val() ) );
   }
 });
});

请注意,当没有找到 clipboardData 对象时(在 IE 以外的浏览器上),您当前正在获取元素的完整值 + 剪贴板的值。

如果您真的只是在真正将数据粘贴到元素中之后,您可能可以做一些额外的步骤来区分两个值,在输入之前和输入之后。

于 2009-03-26T18:57:05.327 回答
6
 $('').bind('input propertychange', function() {....});                      

这将适用于鼠标粘贴事件。

于 2014-10-07T16:49:14.757 回答
5

如何比较字段的原始值和字段的更改值,并将差值作为粘贴值扣除?即使字段中存在现有文本,这也会正确捕获粘贴的文本。

http://jsfiddle.net/6b7sK/

function text_diff(first, second) {
    var start = 0;
    while (start < first.length && first[start] == second[start]) {
        ++start;
    }
    var end = 0;
    while (first.length - end > start && first[first.length - end - 1] == second[second.length - end - 1]) {
        ++end;
    }
    end = second.length - end;
    return second.substr(start, end - start);
}
$('textarea').bind('paste', function () {
    var self = $(this);
    var orig = self.val();
    setTimeout(function () {
        var pasted = text_diff(orig, $(self).val());
        console.log(pasted);
    });
});
于 2013-05-06T18:15:42.707 回答
5

此代码对我有用,可以通过右键单击粘贴或直接复制粘贴

   $('.textbox').on('paste input propertychange', function (e) {
        $(this).val( $(this).val().replace(/[^0-9.]/g, '') );
    })

当我粘贴Section 1: Labour Cost它变成1在文本框中。

为了只允许浮点值,我使用此代码

 //only decimal
    $('.textbox').keypress(function(e) {
        if(e.which == 46 && $(this).val().indexOf('.') != -1) {
            e.preventDefault();
        } 
       if (e.which == 8 || e.which == 46) {
            return true;
       } else if ( e.which < 48 || e.which > 57) {
            e.preventDefault();
      }
    });
于 2014-12-07T16:34:20.050 回答
4
document.addEventListener('paste', function(e){
    if(e.clipboardData.types.indexOf('text/html') > -1){
        processDataFromClipboard(e.clipboardData.getData('text/html'));
        e.preventDefault();

        ...
    }
});

更远:

于 2014-12-23T05:44:28.750 回答
3

见这个例子:http ://www.p2e.dk/diverse/detectPaste.htm

它本质上使用 oninput 事件跟踪每个更改,然后通过字符串比较检查它是否是粘贴。哦,在 IE 中有一个 onpaste 事件。所以:

$ (something).bind ("input paste", function (e) {
    // check for paste as in example above and
    // do something
})
于 2009-03-26T18:45:50.627 回答
1

此方法使用 jquery contents().unwrap()。

  1. 一、检测粘贴事件
  2. 向我们要粘贴的元素中已经存在的标签添加一个唯一的类。
  3. 在给定的超时后扫描所有内容,展开没有您之前设置的类的标签。注意:此方法不会像
    以下示例一样删除自闭合标签。

    //find all children .find('*') and add the class .within .addClass("within") to all tags
    $('#answer_text').find('*').each(function () {
    $(this).addClass("within");
    });
    setTimeout(function() {
    $('#answer_text').find('*').each(function () {
        //if the current child does not have the specified class unwrap its contents
        $(this).not(".within").contents().unwrap();
    });
    }, 0);
    
于 2011-06-19T16:12:34.413 回答
1

使用类 portlet-form-input-field 从所有字段中删除特殊字符的脚本:

// Remove special chars from input field on paste
jQuery('.portlet-form-input-field').bind('paste', function(e) {
    var textInput = jQuery(this);
    setTimeout(function() {
        textInput.val(replaceSingleEndOfLineCharactersInString(textInput.val()));
    }, 200);
});

function replaceSingleEndOfLineCharactersInString(value) {
    <%
        // deal with end-of-line characters (\n or \r\n) that will affect string length calculation,
        // also remove all non-printable control characters that can cause XML validation errors
    %>
    if (value != "") {
        value = value.replace(/(\x00|\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0B|\x0C|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E|\x1F|\x7F)/gm,'');
        return value = value.replace(/(\r\n|\n|\r)/gm,'##').replace(/(\#\#)/gm,"\r\n");
    }
}
于 2012-12-16T22:32:11.217 回答
0

这被证明是非常虚幻的。在粘贴事件函数内的代码执行之前,输入的值不会更新。我尝试从粘贴事件函数中调用其他事件,但输入值仍未使用任何事件函数内的粘贴文本更新。这是除了 keyup 之外的所有事件。如果您从粘贴事件函数中调用 keyup,您可以从 keyup 事件函数中清理粘贴的文本。像这样...

$(':input').live
(
    'input paste',
    function(e)
    {
        $(this).keyup();
    }
);

$(':input').live
(
    'keyup',
    function(e)
    {
        // sanitize pasted text here
    }
);

这里有一个警告。在 Firefox 中,如果您在每个键位上重置输入文本,如果文本长于输入宽度允许的可视区域,则在每个键位上重置值会破坏浏览器功能,该功能会自动将文本滚动到插入符号位置文本的结尾。相反,文本滚动回到开头,使插入符号不在视野范围内。

于 2009-07-22T17:44:45.520 回答
-2

这里有一个警告。在 Firefox 中,如果您在每个键位上重置输入文本,如果文本长于输入宽度允许的可视区域,则在每个键位上重置值会破坏浏览器功能,该功能会自动将文本滚动到插入符号位置文本的结尾。相反,文本滚动回到开头,使插入符号不在视野范围内。

function scroll(elementToBeScrolled) 
{
     //this will reset the scroll to the bottom of the viewable area. 
     elementToBeScrolled.topscroll = elementToBeScrolled.scrollheight;
}
于 2010-05-21T12:18:01.243 回答