Php 如何选择使用name属性动态生成的元素
我有一个php代码,用于论坛页面,在发布评论时动态生成回复按钮,如下所示:Php 如何选择使用name属性动态生成的元素,php,jquery,html,Php,Jquery,Html,我有一个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 = $s
<?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/>';
}
}
?>
我一直在控制台中发现语法错误。请有人指导我。我知道有什么地方我做错了,但我无法准确地理解。Id需要是唯一的-您当前正在将所有设置为相同的(post_Id),这看起来有点奇怪。我想应该是这样的:
“button[name=”+postId+“]”“
@gavgrif谢谢。但您所引用的id是一个隐藏字段,用于回显焦点中当前注释的id。在其中一个表单中,您还有一个勾号而不是引号elements@Fred-ii已修复,但不是错误的原因。
var postId = $('form input#post_id').val();
$(document).on('click', 'button[name="+postId+"]', function(e){
e.preventDefault();
alert(“worked!”);