我的动态选择选项有问题。我的问题是我的选择选项没有显示任何内容,只是显示空白。我已经尝试了很多次,但我被困住了
这是我的 HTML 选择选项代码
<select name="jumlahpesan" id="jumlahpesan" data-native-menu="false">
<option value="choose-one" data-placeholder="true">Choose one... </option>
</select>
这是我的 ajax 代码,用于获取值以填写我的选择选项
$.ajax({
url: host+'/skripsi3/phpmobile/cekjumlah.php',
data: { "id": getacara},
dataType: 'json',
success: function(data, status){
$.each(data, function(i,item){
$("#jumlahpesan").append('<option value="'+item.jumbros+'">"'+item.jumbros+'"</option>').trigger("create")
});
},
error: function(){
//output.text('There was an error loading the data.');
}
});
最后,这是我的"切克朱姆拉.php"
<?php
session_start();
include "config.php";
$idacara=mysql_real_escape_string($_GET["id"]);
$arr = array();
$result=mysql_query("select jumlahpesan from acara where id_acara='$idacara'");
if (!empty($result))
{
while ($row=mysql_fetch_array($result))
{
$tempjum = $row['jumlahpesan'];
for($i=0;$i<$tempjum;$i++)
{
$fetchkategori[] = array
(
'jumbros' => $i,
);
}
}
}
mysql_close($con);
header('Content-Type:application/json');
echo json_encode($fetchkategori);
?>
我想从"Cekjumlah.php"的循环中填充我的选择选项,并使用我的ajax调用我的PHP。谢谢
这是我的阿贾克斯回应
[{"jumbros":0},{"jumbros":1},{"jumbros":2}]
我真的无法在 PHP 方面帮助你,所以如果那里有问题,我看不到它。
但是您似乎错误地使用了 .each(),因为每个元素都旨在迭代元素的 jquery 选择并为每个元素执行功能,而您似乎试图将其用作数据上的 foreach
我认为你必须用 $.each 替换
for item in data{
$("#jumlahpesan").append('<option value="'+item.jumbros+'">"'+item.jumbros+'"</option>').trigger("create")
}
(我假设数据是一个JSON数组,你可能必须首先使用JSON.parse(data)来判断你的帖子上的一些评论。