我有一个论坛页面的php代码,当评论发布时动态生成回复按钮,如下所示:
<?php
require("includes/conn.php");
$stmt=$conn->prepare("SELECT post_id, user, topic, post, time FROM post_tb ORDER BY time DESC");
$stmt->execute();
$result = $stmt->get_result();
$num_of_rows = $result->num_rows;
if ($num_of_rows > 0){
while ($row = $result->fetch_assoc()){
$post_id = $row['post_id'];
$user = $row['user'];
$topic = $row['topic'];
$post = $row['post'];
$time = $row['time'];
$time = date('M dS, Y g:i A', strtotime($time));
echo '<div>
<div>
<h5><strong>'.$user.'</strong></h5><h6>'.$time.'</h6>
<h5><strong>'.ucfirst($topic).'</strong></h5>
<p data-id="'.$post_id.'">'.$post.'</p>
</div>
<div>
<button type="button" class="btn btn-primary rep" name="'.$post_id.'">Reply</button>
</div>
<form id="comment_reply" data-id="'.$post_id.'" method="post" >
<input type="text" class="hidden" value="'.$post_id.'" id="post_id">
<input type="text" class="hidden" value="'.$user.'" id="user">
<textarea class="form-control" rows="3" name="’.$post_id.'" "></textarea>
<button type="submit" class="btn btn-primary" name = “'.$post_id.'” id="post_rep_sub">Submit</button>
</form>
<div/>';
}
}
?>
现在我需要在jquery中的事件处理中选择带有name属性的回复按钮(因为它是动态生成的唯一性):
var postId = $('form input#post_id').val();
$(document).on('click', 'button[name="+postId+"]', function(e){
e.preventDefault();
alert(“worked!”);
我在控制台中不断收到语法无效错误。有人请指导我。我知道有些事情我做错了但却无法弄明白。