簡體   English   中英

Jquery Chosen Select插件在PHP Ajax響應中不起作用

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

我有兩個選擇的選擇框用於從MYSQL DB獲取值

當No-1 Select框改變時,No-2選擇框從AJAX響應生成。

No-1中的Chosen插件完美運行。 但是當從ajax生成No-2選擇框時,選擇的插件在No-2選擇框中不起作用。

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代碼:

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>";


}

讓我知道我做錯了什么.. ??

假設您在第二個列表中選擇了呼叫,請嘗試使用

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

更新其列表后。

如果你看一下插件頁面就表明了這一點

`您可以在原始選擇字段上觸發多個事件以調用選擇中的行為。

selected:updated每當選擇的基礎選擇元素發生更改(例如所選選項中的更改)時,應觸發此事件

您需要使用新數據重建選擇框。

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM