简体   繁体   English

将多个文本行粘贴到多个html输入框中

[英]Paste multiple text lines into multiple html input boxes

I am looking for a way to let the users copy eg a multi-line address paste it into a webpage where there is one input field for each address line in such a way that not only the first line is pasted, but instead the program automatically jumps to the next field and put line 2 in there and so on. 我正在寻找一种让用户复制的方法,例如将多行地址粘贴到网页中,其中每个地址行都有一个输入字段,这样不仅粘贴第一行,而且程序会自动粘贴跳到下一个字段,并在其中放置第2行,依此类推。

Below is the code I managed to find, But it's limited to word counts.. In the below code it's 1.. Is there any way which won't limit by word count but by line count 下面是我好不容易才找到的代码,但它仅限于字数。在下面的代码是1 ..有没有什么办法,这将不是字数 ,而是由行数 限制

        <input type="text" class="text-input" name="box1"> 
        <input type="text" class="text-input" name="box2"> 


  function handleCharacter(event) {
    var $input = $(this),
        index = getIndex($input),
        digit = $input.val().slice(0,1),
        rest = $input.val().slice(1),
        $next;

    if (rest.length > 0) {
        $input.val(digit);  // trim input value to just one character
        $next = $('.text-input[name="chars['+ (index + 1) +']"]');

        if ($next.length > 0) {
            $next.val(rest);  // push the rest of the value into the next input
            $next.focus();
            handleCharacter.call($next, event);  // run the same code on the next input
        }
    }
}

function handleBackspace(event) {
    var $input = $(this),
        index = getIndex($input),
        $prev;

    // if the user pressed backspace and the input is empty
    if (event.which === 8 && !$(this).val()) {
        $prev = $('.def-txt-input[name="chars['+ (index - 1) +']"]');
        $prev.focus();
    }
}

function getIndex($input) {
    return parseInt($input.attr('name').split(/[\[\]]/)[1], 10);
}

$('.def-txt-input')
    .on('keyup', handleCharacter)
    .on('keydown', handleBackspace);

Thanks in advance for your help!!!! 在此先感谢您的帮助!!!!

I provided a textarea to paste stuff and then split the lines and pasted in other boxes. 我提供了一个textarea,用于粘贴内容,然后拆分行并粘贴到其他框中。

    <textarea class="def-txt-input" id="mainbox">

    </textarea>

    <br/>
    <input type="text" class="text-input" name="box1"> 
    <input type="text" class="text-input" name="box2"> 
    <input type="text" class="text-input" name="box3">
    <input type="text" class="text-input" name="box4">
    <input type="text" class="text-input" name="box5">
    <input type="text" class="text-input" name="box6">



        <script>

            function handleCharacter(event) {
                var longString = $("#mainbox").val();
                var ks = $('#mainbox').val().split("\n");
                //e.preventDefault();

                var inputs = $(".text-input");

                $.each(ks, function(k){
                   //alert(ks[k]);
                   //alert(inputs[k].name);
                   var thisName = inputs[k].name;
                   //$("[name='box1']").val('hi');

                   $('[name="' + thisName + '"]').val(ks[k]);


                });

                console.log(longString);



            }

            $('.def-txt-input')
                .on('keyup', handleCharacter);
               // .on('keydown', handleBackspace);


        </script>

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

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