javascript - Uncaught ReferenceError: $this is not defined ajax -


i'm new in javascript , ajax. want make dynamic select option list this.

enter image description here

but there error when try compile using google chrome developer (press f12).

enter image description here

here script :

<div class="container">          <div class="row">             <div class="col-md-offset-3 col-lg-6">                 <h1 class="text-center">ajax & codeigniter select box dependent</h1>                 <div class="form-group">                     <label for="country">country</label>                     <select class="form-control" name="country" id="country">                         <option value="">select country</option>                         <?php foreach ($countries $country) : ?>                              <option value="<?php echo $country->country_id; ?>"><?php echo $country->country_name; ?></option>                         <?php endforeach; ?>                     </select>                   </div>                   <div class="form-group">                     <label for="pwd">province:</label>                     <select class="form-control" name="province" id="province" disabled="">                         <option value="">select province</option>                     </select>                   </div>             </div>         </div>         <!-- /.row -->      </div>     <!-- /.container -->      <!-- jquery version 1.11.1 -->     <script src="http://code.jquery.com/jquery-1.11.1.js"></script>      <!-- bootstrap core javascript -->     <script src="<?php echo base_url() ?>assets/js/bootstrap.min.js"></script>     <script type="text/javascript">         $(document).ready(function(){             $('#country').on('change',function(){                 var country_id = $($this).val();                 if(country_id == '')                 {                     $('#province').prop('disabled',true);                 }                 else                 {                     $('#province').prop('disabled',false);                 }             });         });     </script> </body>  </html> 

if know wrong code, please me. thanks

there typo, probably:

$('#country').on('change',function(){     var country_id = $($this).val();     //                 ^ remove $ sign     ... }) 

replace $($this) $(this), because didn't define $this. this (without $) context.


also, improvement, can remove if , repetitive code doing:

$('#country').on('change',function(){   var country_id = $(this).val();   $('#province').prop('disabled', country_id == ''); }); 

furthermore, can directly:

$('#country').on('change',function(){   $('#province').prop('disabled', this.value == ''); }); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -