マイPHP HTMLコードはこれです:jqueryのは、ちょうど最新のボタンのために働くいないすべてのボタン
<?php
foreach($questions as $question){
echo"
<div id='postShare'>
<div class='col-md-10 col-md-offset-1' id='listPost'>
<div class='list-group'>
<li class='list-group-item text-center '>
<span class='pull-left' id='question_author'>".$question->author."</span>
<span class='pull-right' id='question_time'><p>".time_ago($question->time)."</p></span>
<hr>
<span class='pull-right' style='color:red;font-size:11px;'>سوال</span>
<br>
<center><h4 id='question_title'>" .$question->title. "</h4></center>
<hr>
<span class='pull-right' style='color:blue;font-size:11px;'>توضیحات</span>
<br>
<h5 id='question_desc'>".$question->description."</h5>
<hr>
<span class='pull-right' style='color:GoldenRod;font-size:11px;'>کد برنامه</span>
<br>
<pre id='question_code'>".$question->code."</pre>
<hr>
<button class='btn btn-primary' id='send_answer'>پاسخگویی به این سوال</button>
</li>
</div>
</div>
</div>";
}
?>
と私のjQueryのファイルはこれです:ちょうどデータベースを取得し、表示し
$(function(){
$("#send_answer").click(function(e){
alert("cliked");
e.preventDefault();
});
});
このPHPコードデータをボタンで表示します。 そのボタンをクリックすると警告が表示されます。フェッチしてすべての人のために働かない最新のデータのための仕事。
あなたのHTMLに 'id'値を複製しているため。これは無効なので、JavaScriptの動作は未定義です。 – David
これは違法です。あなたは同じIDのボタンを作成しています。ループされた要素にはidではなく、クラスを使用します。 –
@SterlingArcher "違法"ですか? 「無効」という意味ですか? –