mercredi 2 août 2017

jquery calling variable after ajaxcomplete

The whole idea is to limiting the number checkboxes through dropdown, the approach is: I have dropdown with following code

<select name="form[norequnit][]" id="norequnit" class="rsform-select-box">
  <option value="">...</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
</select>
<input name="chk" type="checkbox" class="bd-lable"  value="9432"><label>01</label>
<input name="chk" type="checkbox" class="bd-lable"  value="9432"><label>02</label>
<input name="chk" type="checkbox" class="bd-lable"  value="9432"><label>03</label>
<input name="chk" type="checkbox" class="bd-lable"  value="9432"><label>04</label>
<input name="chk" type="checkbox" class="bd-lable"  value="9432"><label>05</label>
<input name="chk" type="checkbox" class="bd-lable"  value="9432"><label>06</label>

And some check boxes which are loading by ajax and below code is running to get dropdown value and also after ajax part to limit the number of selection based on the selected dropdown,

<script type="text/javascript">
 jQuery(document).ready(function($) {

 $("#norequnit").on("change", function () {
 $('#unitcount').html($(this).find('option:selected').text());
 });

 $( document ).ajaxComplete(function() {

     $( ".log" ).text( "Triggered ajaxComplete handler." );

        var nux = $('#unitcount').text();

        $("input[name=chk]").change(function(){

    var max= nux;

    if( $("input[name=chk]:checked").length == max )
        {
            $("input[name=chk]").attr('disabled', 'disabled');
            $("input[name=chk]:checked").removeAttr('disabled');
        }           
        else{
            $("input[name=chk]").removeAttr('disabled');
            }
        })
   });

   });
  </script>

Problem:

variable "nux" get value only in first attempt by selecting dropdown, for example 5 so you to limit the boxes to 5 checks, but after this if you change dropdown to any other number the checkbox limitation remains on 5, in other word "nux" wont get new variable.




Aucun commentaire:

Enregistrer un commentaire