我正在尝试使用 PHP 和 jQuery 从 MySQL 数据库获取拼贴的各个部分。
我得到的数据正常,但当我使用 html()
将结果添加到 div
这是我的代码:
$("#collage_id").on('change',function(e){
$("#sections").fadeOut(1200);
if(this.value==0){
$("#sections").fadeOut(1200);
}
else {
<?PHP
$collage_sections=new DataBase();
$sections=$collage_sections->get_sections();
$sections_checkbox="";
while($section=mysqli_fetch_array($sections)){
$sections_checkbox.="<label class='checkbox-inline'><input type='checkbox' value='$section[0]'>$section[1]</label>";
}
?>
$("#sections").html(<?PHP echo $sections_checkbox; ?>).fadeIn(1200);
}
});
有人有任何帮助或想法吗?
最佳答案
正如@Swellar所说,将$sections_checkbox
变量用引号引起来。目前,javascript 将其视为变量而不是您想要的字符串。提示:请始终在浏览器中使用控制台。您将找到大多数语法错误的答案。要修复您的错误,请执行此操作。
$("#sections").html("<?php echo $sections_checkbox; ?>").fadeIn(1200);
关于javascript - 使用 jQuery html() 函数将 PHP 变量传递给 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518270/