简体   繁体   中英

Jquery Chosen Select plugin doesn't Work in PHP Ajax response

I have two chosen select box for fetching values from MYSQL DB

When No-1 Select box change then No-2 select box generate from AJAX response.

Chosen Plugin in No-1 work perfectly. But When No-2 select box generate from ajax then chosen plugin doesn't work in No-2 select box.

Main.PHP

         <div class="control-group">
          <label for="textfield" class="control-label">Category<span>*</span></label>
              <div class="controls">
              <select name="category" id="category" multiple="multiple" class="chosen-select input-medium" required onchange="showSubCat(this.value)">
                <option value='1'>Option 1</option>
                <option value='2'>Option 2</option>
              </select>
              <input type='hidden' value='' id='categoryId' name='categoryId'>
             </div>
            </div>  

            <div class="control-group">
              <label for="textfield" class="control-label">Sub Category</label>
              <div class="controls">
              <select name="subCat_2" id="subCat_2" multiple="multiple" class="chosen-select input-large">
                <option value="">--- Select Sub Category---</option>
              </select>
             </div>
            </div>

JS CODE :

var xmlhttp;
        function showSubCat(str){

            if( $('#category :selected').length > 0){
            //build an array of selected values
            var selectednumbers = [];
            $('#category :selected').each(function(i, selected) {
                selectednumbers[i] = $(selected).val();
            });
            }
            //alert(selectednumbers);   
            document.getElementsByName('categoryId')[0].value = selectednumbers;
            if (str==""){
                document.getElementById("subCat_2").innerHTML="";
                return;
            }
            if (window.XMLHttpRequest){
                xmlhttp=new XMLHttpRequest();
            }
            else{
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("subCat_2").innerHTML=xmlhttp.responseText;
                    //alert(xmlhttp.responseText);
                    console.log(xmlhttp.responseText);
                }
            }
            xmlhttp.open("GET","MYurl.php?catId="+selectednumbers,true);
            xmlhttp.send();
        }

AJAX.PHP

echo "<option value=''>--- Select Sub Category --- </option>";


        $sql="SELECT field FROM tableName WHERE condition;
        $result = mysql_query($sql) or die('MySql Error' . mysql_error());
        while($row = mysql_fetch_array($result)){

            echo "<option value='1'> ----- Sub Option 1</option>";


}

Let me know what i have done Wrong..??

Assuming you're calling chosen on the 2nd list, try using

$('#subCat_2').trigger('chosen:updated');

after you update its list.

If you look at plugin page it indicates that

`You can trigger several events on the original select field to invoke a behavior in Chosen.

chosen:updated This event should be triggered whenever Chosen's underlying select element changes (such as a change in selected options).`

You need to rebuild the selectbox with new data in it.

            if (xmlhttp.readyState==4 && xmlhttp.status==200){

                // bind new data
                document.getElementById("subCat_2").innerHTML=xmlhttp.responseText;

                // refresh chosen.
                $('#subCat_2').trigger("chosen:updated");
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM