2016-09-20 9 views
0

マイ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> 
        &nbsp; 
        <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コードデータをボタンで表示します。 そのボタンをクリックすると警告が表示されます。フェッチしてすべての人のために働かない最新のデータのための仕事。

+2

あなたのHTMLに 'id'値を複製しているため。これは無効なので、JavaScriptの動作は未定義です。 – David

+0

これは違法です。あなたは同じIDのボタンを作成しています。ループされた要素にはidではなく、クラスを使用します。 –

+0

@SterlingArcher "違法"ですか? 「無効」という意味ですか? –

答えて

3

同じclassのボタンを作成する必要があります。同じidで作成すると、1つしか操作できません。

PHP

<?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>&nbsp; 
          <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 send_answer'>پاسخگویی به این سوال</button> 
         </li> 
        </div> 
       </div> 
      </div>"; 
    } 
?> 

jQueryの

$(function() { 
    $(".send_answer").click(function(e) { 
     alert("cliked"); 
     e.preventDefault(); 
    }); 
}); 
+0

ありがとうございました。それは動作します –

+0

助けて嬉しい:) –

+0

親切に答えを受け入れる –

関連する問題