手风琴赢得了';加载新内容时无法工作


accordion won't work with new content loaded

我已经试着让它工作了一段时间。

当我将新的Ajax内容加载到手风琴中时,新内容将不起作用。预加载的内容在之前和之后都能正常工作。我已在此处添加代码我知道你不能用ajax运行这个脚本,因为我的配置和mysql都是本地运行的。

这是我的"update-data.php":

<?php
include('../../includes/config.inc.php');
if(isSet($_POST['content']))
{
$content=$_POST['content'];
$name=$_POST['name'];
$query = "INSERT INTO messages(msg,name) VALUES ('$content','$name')";
mysqli_query($sqlCon, $query);
//mysqli_query("insert into messages(msg) values ('$content')");
$sql_in= mysqli_query($sqlCon, "SELECT msg,msg_id,name FROM messages order by msg_id desc");
$r=mysqli_fetch_array($sql_in);
$msg=$r['msg'];
$name=$r['name'];
$msg_id=$r['msg_id'];
}
?>
<div class="accordionButton"><?php echo $msg_id; ?>:<?php echo $name; ?></div>
<div class="accordionContent" style="display: block;"><?php echo $msg; ?></div>

感谢您的帮助

以下是ajax调用:

<script type="text/javascript">
$(function() {
    $(".comment_button").click(function() 
    {
    var element = $(this);
    var boxval = $("#content").val();
    var bval = $("#name").val();
    var dataString = {content:boxval,name:bval};
    if(boxval=='')
    {
    alert("Please Enter Some Text");
    } else {
    $("#flash").show();
    $("#flash").fadeIn(400).html('<img src="ajax.gif" align="absmiddle">&nbsp;<span class="loading">Loading Update...</span>');
    $.ajax({
        type: "POST",
        url: "<?php echo $total_path.'/update_data.php'; ?>",
        data: dataString,
        cache: false,
        success: function(html){
        $("div#wrapper_ac").prepend(html);
        $("div#wrapper_ac .accordionButton:first").slideDown("slow");
        document.getElementById('content').value='';
        document.getElementById('name').value='';
        $("#flash").hide();
        }
    });
    }
return false;
});
</script>

你的php很好,只需清理你的输入并查看PDO

你可以在这里阅读关于清洁输入和PDO在这里

在你的js中,我认为你的问题是你的对账单

 $('.accordionButton').on('click', function() {
    // DO stuff
 }); 

我认为它只是没有在DOM中冒泡到足以捕获新数据的程度,而是在所有手风琴按钮上添加了点击事件并监听它们。

将其更改为此

 $('#wrapper_ac').on('click', '.accordionButton', function() {
    // DO stuff
 }); 

这将侦听器放置在#wrapper_ac上,因此下面发生的任何单击事件都将被捕获。

希望这能帮助

编辑:有关PDO的更多信息,请查看此网站http://www.phptherightway.com/#databases