简体   繁体   English

jQuery输入与textarea粘贴不同的行为

[英]jQuery input vs textarea on paste different behaviour

I have been struggling to find a definite solution for this problem with paste on input. 我一直在努力寻找一个明确的解决方案,在输入上粘贴此问题。

I have got the following regex control on my input 我在输入中得到了以下正则表达式控件

var match = str.match(/^[^,]*,[^,]*,.*$/mg);

You can see the example of its usage on the following demo link. 您可以在以下演示链接上查看其用法示例。 My problem is when I paste a content like 我的问题是当我粘贴类似的内容时

123, john smith, jack david
321, jackie ronal, david blah

into an input field, I get the whole content as a single entry whereas in the textbox it recognizes the new line and that is exactly what I am looking for (as you can see in the demo I procided). 输入字段中,我将全部内容作为一个条目输入,而在文本框中,它可以识别新行,而这正是我要查找的内容(如您所演示的演示中所见)。

Now, can anyone give me a nice solution to get this problem solved with input field would be great otherwise if I have to use textarea I need to find a way to make the textarea borders hidden but as far as my understanding there is no cross browser solution for it to do it nicely (hid all borders and resize icon in all browsers). 现在,任何人都可以给我一个很好的解决方案来解决输入字段问题的方法会非常有用,否则,如果我必须使用textarea,我需要找到一种隐藏textarea边框的方法,但据我了解,还没有跨浏览器解决方案,使其表现出色(在所有浏览器中隐藏所有边框并调整图标大小)。

Here is the demo 这是演示

http://jsfiddle.net/TCMcp/3/ http://jsfiddle.net/TCMcp/3/

Any help would be appreciated. 任何帮助,将不胜感激。

demo 演示

Instead of an input, you can use a div with contenteditable set to true (this is technically incorrect, but provides support for IE). 除了输入之外,还可以使用contenteditable设置为true的div(从技术上讲这是不正确的,但是提供了对IE的支持)。 Example styles can be found in the fiddle. 在小提琴中可以找到示例样式。

<div contenteditable="true" id="input"></div>

We can then get the text content using JavaScript. 然后,我们可以使用JavaScript获取文本内容。

$('#input').on('paste', function () {
    setTimeout(function () {
        var str = $('#input').text();
        jsprint("pasted", str);
        $('#input').text(str); 
        var match = str.match(/^[^,]*,[^,]*,.*$/mg);             

    }, 100);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM