简体   繁体   English

如何使用jQuery在输入中用逗号替换换行符?

[英]How to replace newline with comma in input using jQuery?

I have an input我有一个输入

<input>

When someone pastes multi-line text into the input, I need the newlines ( \\r , \\n , and \\r\\n ) and tabs \\t replaced with a comma , .当有人将多行文本粘贴到输入中时,我需要将换行符( \\r\\n\\r\\n )和制表符\\t替换为逗号, Most of the time, the input will be coming from Microsoft Excel or Apple Numbers when a user copies a column or a row.大多数情况下,当用户复制一列或一行时,输入将来自 Microsoft Excel 或 Apple Numbers。

I am currently trying this jQuery我目前正在尝试这个 jQuery

$(function(){
    // jQuery paste event. how trendy!
    $('input').on('paste', function(e){
        var e = $(this);
        setTimeout(function(){
            e.val( $.trim(e.val()).replace(/\s+/g, ',') );
        }, 0);
    });
});

The problem问题

Some cells in their spreadsheet might have spaces in them so /\\s+/g is going to be too broad.电子表格中的某些单元格中可能有空格,因此/\\s+/g会太宽泛。 However, /[\\r\\n\\t]+/g isn't working.但是, /[\\r\\n\\t]+/g不起作用。 In fact, I don't think jQuery even has access to the input's value before the newlines are being removed.事实上,我认为 jQuery 在删除换行符之前甚至无法访问输入的值。

Sing along on jsFiddle在 jsFiddle 上一起唱歌

I've provided a jsfiddle with expected inputs and outputs我提供了一个带有预期输入和输出的jsfiddle

<!-- sample input, copy below this line --
one
2
hello world
foo bar
m3000
-- end copy -->

<!-- output should look like
one, 2, hello world, foo bar, m3000
-->

You can redirect the event to a textarea and then get the paste value which will content the \\n characters:您可以将事件重定向到 textarea,然后获取将满足 \\n 字符的粘贴值:

$(function(){
    $('input').on('paste', function(e){
        var target = $(e.target);
        var textArea = $("<textarea></textarea>");
        textArea.bind("blur", function(e) {
            target.val(textArea.val().replace(/\r?\n/g, ', ') );
            textArea.remove();
        });
        $("body").append(textArea);
        textArea.focus();
        setTimeout(function(){
            target.focus();
        }, 10);
    });
});

You can try it here: http://jsfiddle.net/dE3At/你可以在这里试试: http : //jsfiddle.net/dE3At/

I am afraid this is not possible (without hacking around).恐怕这是不可能的(没有黑客攻击)。

I suggest you use a textarea , so you can maintain those newlines and do whatever you want to them.我建议你使用textarea ,这样你就可以维护这些换行符并对它们做任何你想做的事情。 You can style the textarea to look like an input, disable resizing and make it have only one row.您可以将textarea为看起来像一个输入,禁用调整大小并使其只有一行。

jsFiddle Demo with textarea带有textarea jsFiddle 演示

What about having a transparent textarea over the input?在输入上有一个透明的 textarea 怎么样? Size it to match.调整大小以匹配。 Once pasted, breaking up newlines should get a lot easier.粘贴后,拆分换行符应该会容易得多。 on focus, raise the text to the textarea level to manipulate your newlines and such, and on blur, send it back to the input.在焦点上,将文本提升到 textarea 级别以操纵换行符等,并在模糊时将其发送回输入。 If maintaining the input is not necessary, then replace it with the textarea styled to look like a textbox.如果不需要维护输入,则将其替换为样式看起来像文本框的 textarea。

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

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