我正在寻找一种方法来清理我粘贴到浏览器中的输入,这可能与 jQuery 相关吗?
到目前为止,我已经设法想出了这个:
$(this).live(pasteEventName, function(e) {
// this is where i would like to sanitize my input
return false;
}
不幸的是,由于这个“小”问题,我的发展陷入了困境。如果有人能指出我正确的方向,我真的会让我成为一个快乐的露营者。
好的,只是遇到了同样的问题..我走了很长一段路
$('input').on('paste', function () {
var element = this;
setTimeout(function () {
var text = $(element).val();
// do something with text
}, 100);
});
在 .val() 函数可以被填充之前只是一个小的超时。
E.
您实际上可以直接从事件中获取价值。但是,如何到达它有点迟钝。
如果您不希望它通过,请返回 false。
$(this).on('paste', function(e) {
var pasteData = e.originalEvent.clipboardData.getData('text')
});
为了跨平台兼容性,它应该处理 oninput 和 onpropertychange 事件:
$ (something).bind ("input propertychange", function (e) {
// check for paste as in example above and
// do something
})
我通过使用以下代码修复了它:
$("#editor").live('input paste',function(e){
if(e.target.id == 'editor') {
$('<textarea></textarea>').attr('id', 'paste').appendTo('#editMode');
$("#paste").focus();
setTimeout($(this).paste, 250);
}
});
现在我只需要存储插入符号的位置并附加到那个位置然后我就准备好了......我想:)
嗯...我认为您可以使用它e.clipboardData
来捕获正在粘贴的数据。如果没有成功,请看这里。
$(this).live("paste", function(e) {
alert(e.clipboardData); // [object Clipboard]
});
监听粘贴事件并设置一个 keyup 事件监听器。在 keyup 上,捕获值并删除 keyup 事件侦听器。
$('.inputTextArea').bind('paste', function (e){
$(e.target).keyup(getInput);
});
function getInput(e){
var inputText = $(e.target).val();
$(e.target).unbind('keyup');
}
$("#textboxid").on('input propertychange', function () {
//perform operation
});
它会正常工作。
这越来越接近您可能想要的。
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 以外的浏览器上),您当前正在获取元素的完整值 + 剪贴板的值。
如果您真的只是在真正将数据粘贴到元素中之后,您可能可以做一些额外的步骤来区分两个值,在输入之前和输入之后。
$('').bind('input propertychange', function() {....});
这将适用于鼠标粘贴事件。
如何比较字段的原始值和字段的更改值,并将差值作为粘贴值扣除?即使字段中存在现有文本,这也会正确捕获粘贴的文本。
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);
});
});
此代码对我有用,可以通过右键单击粘贴或直接复制粘贴
$('.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();
}
});
document.addEventListener('paste', function(e){
if(e.clipboardData.types.indexOf('text/html') > -1){
processDataFromClipboard(e.clipboardData.getData('text/html'));
e.preventDefault();
...
}
});
见这个例子: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
})
此方法使用 jquery contents().unwrap()。
在给定的超时后扫描所有内容,展开没有您之前设置的类的标签。注意:此方法不会像
以下示例一样删除自闭合标签。
//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);
使用类 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");
}
}
这被证明是非常虚幻的。在粘贴事件函数内的代码执行之前,输入的值不会更新。我尝试从粘贴事件函数中调用其他事件,但输入值仍未使用任何事件函数内的粘贴文本更新。这是除了 keyup 之外的所有事件。如果您从粘贴事件函数中调用 keyup,您可以从 keyup 事件函数中清理粘贴的文本。像这样...
$(':input').live
(
'input paste',
function(e)
{
$(this).keyup();
}
);
$(':input').live
(
'keyup',
function(e)
{
// sanitize pasted text here
}
);
这里有一个警告。在 Firefox 中,如果您在每个键位上重置输入文本,如果文本长于输入宽度允许的可视区域,则在每个键位上重置值会破坏浏览器功能,该功能会自动将文本滚动到插入符号位置文本的结尾。相反,文本滚动回到开头,使插入符号不在视野范围内。
这里有一个警告。在 Firefox 中,如果您在每个键位上重置输入文本,如果文本长于输入宽度允许的可视区域,则在每个键位上重置值会破坏浏览器功能,该功能会自动将文本滚动到插入符号位置文本的结尾。相反,文本滚动回到开头,使插入符号不在视野范围内。
function scroll(elementToBeScrolled)
{
//this will reset the scroll to the bottom of the viewable area.
elementToBeScrolled.topscroll = elementToBeScrolled.scrollheight;
}