简体   繁体   English

使用JavaScript / jQuery将制表符后的字符串值剪切并粘贴到下一个输入

[英]Cut and Paste string values to next input after tab character using JavaScript/jQuery

I was wondering how can I achieve, using javascript/jquery, splitting a string as listed below - 我想知道如何实现,使用javascript / jquery,拆分下面列出的字符串 -

abc543567   abc543678   abc223416   abc634567

into four inputs, so that each group can be moved into a separate input. 分为四个输入,这样每个组都可以移动到一个单独的输入中。 Basically, when I copy this string into the first input, the first group can remain in the first input, the second to be moved in the second input, and so on. 基本上,当我将此字符串复制到第一个输入时,第一个组可以保留在第一个输入中,第二个组可以在第二个输入中移动,依此类推。 In the example, between the groups, I used as separator the tab characters. 在示例中,在组之间,我用作制表符的分隔符。 Here is the html for the inputs: https://jsfiddle.net/2sb6ggz0/ 这是输入的html: https//jsfiddle.net/2sb6ggz0/

Update of HTML: 更新HTML:

<table>
<tr>
    <td>
        <form name="form1" action="#" method="get" target="">
            <input id="input1" name="query" placeholder="Input 1" type="search" size="20">
        </form>
    </td>
    <td>
        <form name="form2" action="#" method="get" target="">
            <input id="input2" name="query" placeholder="Input 2" type="search" size="20">
        </form>
    </td> 
    <td>
        <form name="form3" action="#" method="get" target="">
            <input id="input3" name="query" placeholder="Input 3" type="search" size="20">
        </form>
    </td>
    <td>                    
        <form name="form4" action="#" method="get" target="">
            <input id="input4" name="query" placeholder="Input 4" type="search" size="20">
        </form>
    </td>
</tr>
</table>

Is there a way top achieve this? 有没有办法达到顶峰?

You can respond to the paste event by detecting what's in the first input and, if appropriate, moving bits of it to the other three (see comments) : 您可以通过检测第一个输入中的内容来响应paste事件,并在适当的情况下将其移动到其他三个(请参阅注释)

 // Text to paste: abc543567 abc543678 abc223416 abc634567 // Hook "paste" on the first input $("#input1").on("paste", function() { // Remember the input and wait a second so the paste gets filled in var input = this; setTimeout(function() { // See if it matches our format var n; var value = input.value; var m = /^[ \\t]*([\\w\\d]{9})[ \\t]+([\\w\\d]{9})[ \\t]+([\\w\\d]{9})[ \\t]+([\\w\\d]{9})[ \\t]*$/.exec(value); if (m) { // It does, save the values to the other fields for (n = 1; n <= 4; ++n) { $("#input" + n).val(m[n]); } } }, 0); }); 
 <table> <tr> <td> <form name="form1" action="#" method="get" target=""> <input id="input1" name="query" placeholder="Input 1" type="search" size="20"> </form> </td> <td> <form name="form2" action="#" method="get" target=""> <input id="input2" name="query" placeholder="Input 2" type="search" size="20"> </form> </td> <td> <form name="form3" action="#" method="get" target=""> <input id="input3" name="query" placeholder="Input 3" type="search" size="20"> </form> </td> <td> <form name="form4" action="#" method="get" target=""> <input id="input4" name="query" placeholder="Input 4" type="search" size="20"> </form> </td> </tr> </table> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

You'll want to adjust the regular expression to accept the correct format; 您需要调整正则表达式以接受正确的格式; I just told it to allow exactly 9 "word" characters and digits in four groups, separated by one or more tabs or spaces. 我只是告诉它允许四个组中的9个“单词”字符和数字,由一个或多个制表符或空格分隔。

Here is your updated fiddle: https://jsfiddle.net/2sb6ggz0/6/ 这是您更新的小提琴: https//jsfiddle.net/2sb6ggz0/6/

function split(sep, clazz) {

    var items = $(clazz);

    items.each(function (i) {

        $(this).on("paste", function () {
            var me = $(this);
            setTimeout(function () {
                var splitted = me.val().split(sep);
                items.each(function (i) {
                    $(this).val(splitted[i]);
                });
            }, 1);
        });
    })
};    
split("-", ".query-input")

Here is a possible solution. 这是一个可能的解决方案。

$("input[name=query]").on("paste", function() {
    var $this = $(this);
    setTimeout(function() {
        var id = $this.attr("id"), no = parseInt(id.substr(5)),
            groups = $this.val().split(/\s+/);
        if (groups) {
            var i = 0;
            while (no <= 4 && i < groups.length) {
                $("#input" + no).val(groups[i]);
                ++no;
                ++i;
            }
        }
    }, 0);
});

Fiddle here: https://jsfiddle.net/robbyn/rn9ydtop/ 小提琴: https//jsfiddle.net/robbyn/rn9ydtop/

You can use an event listener to skip to the next box when you have typed in a certain number of characters. 键入一定数量的字符后,可以使用事件侦听器跳到下一个框。

In jQuery it would be something like 在jQuery中它会是这样的

$('input').on('keyup', function(e) {
    if (e.target.val().length === 9) {
        $(this).nextAll('input').first().focus();
    }
})

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

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