dongqian6484 2013-08-06 23:38
浏览 84
已采纳

如何使用ajax捕获while循环中的变量?

I have a loop to answer questions that looks something like this:

<?php
while ($u=mysql_fetch_array($result)){
?>     
<table>
 <tr>
     <td>Question_ID</td>
     <td>Question</td>
     <td>Answer</td>
 </tr>
 <tr>
     <td><? echo $u['question_id'];?></td>
     <td><? echo $u['question'];?></td> 
     <td> 
         <form>
         <input type="hidden" value="echo $u['question_id'];?>" />
         <input type="text"/>
         <a href="#" onClick="ajax_answer();">Send Answer</a>
         </form>
     </td>
 </tr>
</table>
<?php
} 
?>  

If the user answers for example the third question that appears on the page, my question is how do I capture the text written and the question_id so I can send those variables to a php page?

<script>
   function ajax_answer(){
       $.ajax({
       question_id = ??? //how do I capture this variable?
       answer = ??? //how do I capture this variable?
       url:'answers.php',
       type:'POST',
       dataType:'text/html',
       data:'question_id='+question_id + '&answer='+answer,
       success: function(){
                          };
             });
       };
</script>

Thanks!

  • 写回答

3条回答 默认 最新

  • dongzan1970 2013-08-07 00:08
    关注

    If you want to do it in pure javascript,
    then pass this to your ajax_answer function from onclick event like below

    <?php
      while( $u = mysql_fetch_array( $result ) ) {
    ?> 
      <tr>
        <td><?php echo $u['question_id'];?></td>
        <td><?php echo $u['question'];?></td> 
        <td>
          <input type="hidden" value="<?php echo $u['question_id'];?>" />
          <input type="text" />
          <a href="#" onclick="ajax_answer( this );">Send Answer</a>
        </td>
      </tr>
    <?php
      }
    ?>
    

    and your javascript will be...

    <script type="text/javascript">
      function ajax_answer( elem ) {
        var question_id = elem.parentElement.children[0].value;
        var answer      = elem.parentElement.children[1].value;
    
        /// do your request here
    
        return false;
      }
    </script>
    

    And the same with jQuery.
    Add name attribute to those input elements, and add a classname to anchor element

    <?php
      while( $u = mysql_fetch_array( $result ) ) {
    ?> 
      <tr>
        <td><?php echo $u['question_id'];?></td>
        <td><?php echo $u['question'];?></td> 
        <td>
          <input type="hidden" name="question_id" value="<?php echo $u['question_id'];?>" />
          <input type="text" name="answer" />
          <a href="#" class="send_answer">Send Answer</a>
        </td>
      </tr>
    <?php
      }
    ?>
    

    Javascript
    add the onclick event handler dynamically. Now your function ajax_answer accepts two parameters question_id and answer, we will pass those two parameters through the click event handler

    <script type="text/javascript">
      $(function() {
        $("a.send_answer").on("click", function( event ) {
          event.preventDefault();
          var td  = $(this).parents("td:first");
          var qid = $("input[name=question_id]", td).val();
          var ans = $("input[name=answer]", td).val();
    
          ajax_answer( qid, ans );
        });
      });
      function ajax_answer( question_id, answer ) {
        /// do your request here
      }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址