简体   繁体   中英

how to validate on form repeater jquery

i have form repeater,and i need to validate specific field before submit.

here's my code

  <div class="col-lg-2" style="margin:0px 5%">
 <div class="form-group">
<label for="amount_split">Amount</label>
<input tclass="form-control split-amount{{ $index }}"
name="split_amount" type="number" id="amount_split">
  </div>
 </div>

the form is inside table, so it keep repeated for every row.

i need whenever any field add cannot submit the form before the addition of all this field is equal some number.

i have tried this

@section('custom_script')
   <script>
 
    var iteration = 0;
    var amount = 0;

     function buttonClicked(ind){
        iteration++;

setTimeout(function(){
        console.log(ind);
        console.log($(".split-amount"+ind));

    var arr=[];
            for (var index = 0; index <= iteration; index++) {
                // console.log(index);
      arr.push($("input[name='moves["+index+"][split_amount]']"));
  
            arr.forEach(function(e){
                $(e).bind('change',function(){
                    // ele.value = 23;
                    // console.log(e[0]);
                    amount += e[0].value;
                    if(amount <= 10){
                        // console.log('yes');
                    }else{
                        // console.log('nooo');

                    }
                });
            });

     }
      
},100);
     }


</script>
     @endsection

here's my answer for this problem

function ind(i){

    $(document).ready(function(){
var el = $(".splite"+i).change(function(){
        var amo = 0;
for(var it = 0;it< el.length;it++){
    amo += parseFloat(el[it].value);
}
console.log(amo);
var am = $("#total"+i);
am = parseFloat(am[0].value);
if(amo == am){
    $("#sub"+i).removeAttr('disabled');
}else{

    $("#sub"+i).attr('disabled',true);
}

$("#split_amount_p"+i).html(amo);
$("#orignal_amount_p"+i).html(am);
if((am - amo) == 0){

$("#difference_amount_p"+i).html(0).css('color','green');
}else{
$("#difference_amount_p"+i).html(am - amo).css('color','red');

}
// console.log(am);
    });
});
}
 function addButton(index){
   ind(index);
 }
  function removeButton(index)
  {
     setTimeout(function(){

   ind(index);
   },500);
 }

</script> 

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