我在显示复选框时遇到问题。我的复选框是动态的,并在ajax成功函数后追加复选框。但我不知道它根本不会显示出来。 这是我的html html和ajax代码
<fieldset id="appendfilter" style="margin-top: 5px;">
</fieldset>
boelanCek = true;
$.ajax({
url: host+'/skripsi3/phpmobile/appendfilter.php',
data: { "id": id},
dataType: 'json',
success: function(data, status){
console.log(data);
$.each(data, function(i,item){
alert("here");
if(item.dicek=="ya")
{
if (boelanCek)
{
if (arrCekbox.indexOf(item.idkategori)<0)
{
$("#appendfilter").append('<input class="kategoriFilter" checked="checked" type="checkbox" value="'+item.nama+'" name="cektambah" id="'+item.idkategori+'"><label for="'+item.idkategori+'">'+item.nama+'</label>');
arrCekbox.push(item.idkategori);
}
boelanCek=false;
}
else
{
if (arrCekbox.indexOf(item.idkategori)<0)
{
$("#appendfilter").prepend('<input class="kategoriFilter" checked="checked" type="checkbox" value="'+item.nama+'" name="cektambah" id="'+item.idkategori+'"><label for="'+item.idkategori+'">'+item.nama+'</label>');
arrCekbox.push(item.idkategori);
}
}
$('#appendfilter').checkboxradio('refresh');
}
else if(item.dicek=="tidak")
{
if (boelanCek)
{
if (arrCekbox.indexOf(item.idkategori)<0)
{
$("#appendfilter").append('<input class="kategoriFilter" type="checkbox" value="'+item.nama+'" name="cektambah" id="'+item.idkategori+'"><label for="'+item.idkategori+'">'+item.nama+'</label>');
arrCekbox.push(item.idkategori);
}
boelanCek=false;
}
else
{
if (arrCekbox.indexOf(item.idkategori)<0)
{
$("#appendfilter").prepend('<input class="kategoriFilter" type="checkbox" value="'+item.nama+'" name="cektambah" id="'+item.idkategori+'"><label for="'+item.idkategori+'">'+item.nama+'</label>');
arrCekbox.push(item.idkategori);
}
}
$('#appendfilter').checkboxradio('refresh');
}
});
},
error: function(){
//output.text('There was an error loading the data.');
}
});
这里面的appendfilter.php
<?php
session_start();
include "config.php";
$user=mysql_real_escape_string($_GET["user"]);
$result=mysql_query("SELECT * from filtering WHERE id_tenant='$user'") or die(mysql_error());
if (!empty($result))
{
while ($row=mysql_fetch_array($result))
{
$tempfilter[] = $row['filter'];
$q="select 'iya' as dicek,kategori.id_kategori,kategori.nama from kategori WHERE id_kategori IN (".implode(',',$tempfilter).") UNION ALL select 'tidak' as dicek,kategori.id_kategori,kategori.nama from kategori where id_kategori NOT IN (".implode(',',$tempfilter).") ";
//echo $q;
$result2 = mysql_query($q) or die(mysql_error());
if (!empty($result2))
{
while ($row2=mysql_fetch_array($result2))
{
$fetchkategori[] = array
(
'nama' => $row2['nama'],
'idkategori' => $row2['id_kategori'],
'dicek' => $row2["dicek"]
);
}
}
}
}
mysql_close($con);
header('Content-Type:application/json');
echo json_encode($fetchkategori);
?>
也许有人可以帮助我。谢谢你们,祝你们度过愉快的一天!干杯!!