How to use AJAX to populate state list depending on Country list?

前端 未结 6 845
我在风中等你
我在风中等你 2020-12-06 15:01

I have the code below that will change a state dropdown list when you change the country list.
How can I make it change the state list ONLY when country ID number 234 an

6条回答
  •  Happy的楠姐
    2020-12-06 15:26

    Just check the countryId value before you do the AJAX request and only perform the request if the countryId is in the allowable range. In the case where the countryId doesn't match, I would hide the select (probably clear it's value, too) and show an already existing input that was previously hidden. The reverse should be done if an allowable country is chosen.

    jQuery example below:

    $(function() { $('#country').change( function() { var val = $(this).val(); if (val == 223 || val == 224) { $('#othstate').val('').hide(); $.ajax({ url: 'findState.php', dataType: 'html', data: { country : val }, success: function(data) { $('#state').html( data ); } }); } else { $('#state').val('').hide(); $('#othstate').show(); } }); });

提交回复
热议问题