Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

javascript - jQuery ie issue with on paste (content with break)

I want to paste a content in an input field (I have to use input field) and get the pasted content to other input. my content is like the following (copy all lines and paste):

1234
4567
4321

on all browser the following link works fine but IE

http://jsfiddle.net/5bNx4/42/

        $editor.on('paste', function() {
var $self = $(this);            
              setTimeout(function(){ 
                var $content = $self.val();             
                $clipboard.val($content);
            },100);
     });

when using IE and when paste the content, only the first line (1234) will be appear into the second input. but other browsers you get all the content.

Can anyone help me out here Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

IE fails to handle new line and it will on only paste in the first line and disregard the rest. Replacing newlines characters with space does the trick.

clipped = clipped.replace(/(
|
|
)/gm, " "); //replace newlines with spaces

To overcome add the below code to your script and this will work fine.

if (window.clipboardData) {
    $('#editor').bind('paste', function (e) {
        var clipped = window.clipboardData.getData('Text');
        clipped = clipped.replace(/(
|
|
)/gm, " "); //replace newlines with spaces
        $(this).val(clipped);
        return false; //cancel the pasting event
    });
}

Check this JSFiddle in IE browser.

Reference: Allow Pasting Multiple Lines in IE Textbox

EDIT: Removed console.log

Updated JSFiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...