Jeremy
Jeremy

Reputation: 973

Using Ajax, change options of select 2 if select 1 changes it's value

So I have two selects. Idea is, when user chooses his option in the first select, based on that, the values of the second select changes, and the values come from a different file using Ajax.

<select id="subject"></select>
<select id="theme"></select>

I tried this, which is not working. Any help ?

$('#subject').change(function(
    var v = $(this).val();
    getTheme(v);
});
function getTheme(id) {
   alert(id);
   $.ajax({
     type: "GET",
     url: 'gettheme.php',
     data: "q=" + id, 
     success: function(data) {
          $('#theme').html(data);
     }
   });
}

Upvotes: 0

Views: 1674

Answers (2)

Md Shawon
Md Shawon

Reputation: 230

jquery fixed. Its working in my server.

Code:

$(document).ready(function(){
$('#subject').change(function(){
   var v = $(this).val();
    getTheme(v);
});
});
function getTheme(id) {
    alert(id);
   $.ajax({
     type: "GET",
     url: "gettheme.php",
     data: "q=" + id, 
     success: function(data) {
          $('#theme').html(data);
     }
   });
}

Upvotes: 1

Tushar Gupta
Tushar Gupta

Reputation: 15933

You mistyped the function syntax use as function(){ I just tried an example to show you the usage

$('#subject').change(function(){
    var v = $(this).val();
    if(v==1)
     $('#theme').val(2);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="subject">
  <option value='0'>Select</option>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>
<select id="theme">
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>
I tried this, which is not working. Any help ?

Upvotes: 1

Related Questions