Chr*_*ist 202 jquery paste sanitize
我正在寻找一种方法来清理我粘贴到浏览器中的输入,这可能与jQuery有关吗?
到目前为止,我已经设法做到了这一点:
$(this).live(pasteEventName, function(e) {
// this is where i would like to sanitize my input
return false;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,由于这个"次要"问题,我的发展已经开始戛然而止.如果有人能指出我正确的方向,我真的会让我成为一个快乐的露营者.
小智 327
好吧,刚刚碰到了同样的问题..我走了很长的路
$('input').on('paste', function () {
var element = this;
setTimeout(function () {
var text = $(element).val();
// do something with text
}, 100);
});
Run Code Online (Sandbox Code Playgroud)
只需要一个小的超时,直到.val()func可以填充.
E.
Cha*_*aro 65
您实际上可以直接从事件中获取值.尽管如此,它有点迟钝.
如果您不希望它通过,则返回false.
$(this).on('paste', function(e) {
var pasteData = e.originalEvent.clipboardData.getData('text')
});
Run Code Online (Sandbox Code Playgroud)
小智 41
对于跨平台兼容性,它应该处理oninput和onpropertychange事件:
$ (something).bind ("input propertychange", function (e) {
// check for paste as in example above and
// do something
})
Run Code Online (Sandbox Code Playgroud)
Chr*_*ist 18
我通过使用以下代码修复它:
$("#editor").live('input paste',function(e){
if(e.target.id == 'editor') {
$('<textarea></textarea>').attr('id', 'paste').appendTo('#editMode');
$("#paste").focus();
setTimeout($(this).paste, 250);
}
});
Run Code Online (Sandbox Code Playgroud)
现在我只需要存放插入位置并追加到那个位置然后我就全部设置了......我想:)
mof*_*off 10
嗯......我想你可以e.clipboardData用来捕捉被粘贴的数据.如果它没有成功,请看看这里.
$(this).live("paste", function(e) {
alert(e.clipboardData); // [object Clipboard]
});
Run Code Online (Sandbox Code Playgroud)
侦听粘贴事件并设置keyup事件侦听器.在keyup上,捕获值并删除keyup事件侦听器.
$('.inputTextArea').bind('paste', function (e){
$(e.target).keyup(getInput);
});
function getInput(e){
var inputText = $(e.target).val();
$(e.target).unbind('keyup');
}
Run Code Online (Sandbox Code Playgroud)
这越来越接近你想要的.
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() ) );
}
});
});
Run Code Online (Sandbox Code Playgroud)
请注意,当找不到clipboardData对象时(在IE以外的浏览器上),您当前正在获取元素的完整值+剪贴板值.
在输入之前和输入之后,如果你真的只是在真正粘贴到元素中的数据之后,你可以做一些额外的步骤来区分这两个值.
小智 6
$("#textboxid").on('input propertychange', function () {
//perform operation
});
Run Code Online (Sandbox Code Playgroud)
它会工作正常.
小智 5
如何比较字段的原始值和字段的更改值,并将差异作为粘贴值减去?即使字段中存在现有文本,也会正确捕获粘贴的文本.
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);
});
});
Run Code Online (Sandbox Code Playgroud)
$('').bind('input propertychange', function() {....});
Run Code Online (Sandbox Code Playgroud)
这适用于鼠标粘贴事件.
这段代码适用于我从右键单击粘贴或直接复制粘贴
$('.textbox').on('paste input propertychange', function (e) {
$(this).val( $(this).val().replace(/[^0-9.]/g, '') );
})
Run Code Online (Sandbox Code Playgroud)
当我粘贴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();
}
});
Run Code Online (Sandbox Code Playgroud)