Pasting multiple numbers over multiple input fields

后端 未结 5 1875
独厮守ぢ
独厮守ぢ 2020-12-17 01:58

I\'ve got a form on my site using 6 input fields. The site visitor simply enters a 6 digit code into these 6 boxes. The thing is that they\'ll get the 6 digit code and it

5条回答
  •  遥遥无期
    2020-12-17 02:28

    HTML

    
    
    
    
    

    jQuery

    $("input").bind("paste", function(e){
       var pastedData = e.originalEvent.clipboardData.getData('text');
       var num_array = [];
       num_array = pastedData.toString(10).replace(/\D/g, '0').split('').map(Number); // creates array of numbers
       for(var a = 0; a < 4; a++) {  // Since I have 4 input boxes to fill in
         var pos = a+1;
         event.preventDefault();
         $('#input-'+pos).val(num_array[a]);
       }
    });
    

提交回复
热议问题