gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 22:54:21 27 4
gpt4 key购买 nike

我有两个选择框用于从 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com