php - Jquery Chosen Select 插件在 PHP Ajax 响应中不起作用

标签 php jquery ajax select jquery-chosen

我有两个选择框用于从 MYSQL 数据库中获取值

当 No-1 选择框改变时,No-2 选择框从 AJAX 响应生成。

No-1 中的 Chosen Plugin 完美运行。但是当 2 号选择框从 ajax 生成时,选择的插件在 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>";


}

让我知道我做错了什么..??

最佳答案

假设您在第二个列表中调用 chosen,请尝试使用

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

更新其列表后。

如果你看plugin page这表明

`您可以在原始选择字段上触发多个事件以调用 Chosen 中的行为。

chosen:updated 只要 Chosen 的底层选择元素发生变化(例如所选选项发生变化),就会触发此事件。`

关于php - Jquery Chosen Select 插件在 PHP Ajax 响应中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26669300/

相关文章:

php - WordPress:使用自定义字段的日期删除帖子

javascript - 如何根据文本添加附加属性

jQuery 对话框 + iframe 在 IE9 中给出此错误 : SCRIPT5009: 'Array' is undefined

javascript - 成功创建对象后,Ajax 刷新 .erb 代码。

javascript - 使用 $.getJSON 将 JSON 对象从数据库传递到 JS

php - 它不会在数据库中插入值

php - 将自定义 Sonata 页面路由添加到导航栏

php - 如何检查没有。使用 MYSQLI_STMT_PREPARE 和 MYSQLI_FETCH_ARRAY 时返回的行数?

javascript - 向上滚动时获取元素的滚动位置

javascript - 手动表 : datas aren't returned to the php file