JQuery Select2 -Chain select - How Pass variable

I am using the Jquery Select2 script http://ivaynberg.imtqy.com/select2/#ajax .

I have two dropdownlist - First choice for regions - Second Choose for cities

Question: How to transfer a variable from the first choice (static content) to the second choice (dynamic Ajax content)

With the second selection, I would like to download only data for one selected region. At the beginning, the second selection is disabled. When the user selects a region, a second selection should be activated.

HTML code

<div class="control-group"><label class="control-label">Region</label> <div class="controls"> <select id="region" name="region"> <option value="">Select region</option> <option value="1">Region 1</option> <option value="2">Region 2</option> </select> </div> </div> <input type="hidden" id="city" name="city" class="input-xlarge" data-placeholder="Choose An Option.." /> 

JavaScript:

 <script type="text/javascript"> $(document).ready(function() { var $region = $('#region'); var $city = $('#city'); $region.change(function() { if ($region.val() == '') { $city.attr('disabled', 'disabled').val(''); $("#city").select2("enable", false); } else { $("#city").select2("enable", true); } }).trigger('change'); }); $('#city').select2({ minimumInputLength: 2, ajax: { url: "index.php?modul=data&reg=2", contentType: 'application/json; charset=utf-8', dataType: 'json', data: function (term, page) { return { q: term, }; }, results: function (data, page) { return { results: data }; } } }); </script> 

Do I need a pass value from the first selection to the variable "reg" Index.php? Modul = data & p = 2 Index.php? Modul = data & p = 2 & d = CITYNAME

+4
source share
1 answer

this is how i did it

  $("#adv_category").bind("change",function() { var adv_category =$("#adv_category option:selected").val(); var datString= 'adv_category='+ adv_category; //alert (dataString);return false; $.ajax({ type: "POST", url: "subcategory_type_format.php", data: datString, success: function(data) { //alert(data); //dyTable(data); $("#advsub_category").html(data).select2(); }, error: function() { alert("TimeOut error !"); var url_r = "index.php"; window.location.href = url_r; } }); return false; }); 

and at startup the 2nd choice has no option.

+1
source

Source: https://habr.com/ru/post/1481382/


All Articles