简体   繁体   中英

assign individual value to text field with same class name using jquery

I want to get value from "data-val" input field which is hypen separated and after removimg hypen individual value should get assigned to separate text box which have same class name, but instead of separate value only last value is assigning to every text box.

for example if data-val class have value like this 3-4-5 then it should get separated by "-" and individual value get assigned to separated text filed.

<input type="text" class="form-control data-val" onblur="assign_val('data-val','input_val')"value=""/>

<input type="text" class="form-control input_val" name="">

 <input type="text" class="form-control input_val"  name="">

<input type="text" class="form-control input_val"  name="">

<script>

    function assign_val(data-val,input_val) {

            var data-val = $(".data-val").val(); 

            var count = $('.' + input_val).length;
            var i;
            var data-val= data-val.split("-");  

            for (i = 1; i <= data-val.length; i++)
            {

              $(".input_val").val(data-val[i]);
            }

        }
    </script>

You can do this using JQuery .each https://api.jquery.com/each/

$( ".input_val" ).each(function( index ) {
  $(this).val(data-val[index]);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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