0

我有一个循环来回答看起来像这样的问题:

<?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
} 
?>  

例如,如果用户回答页面上出现的第三个问题,我的问题是如何捕获写入的文本和 question_id,以便将这些变量发送到 php 页面?

<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>

谢谢!

4

3 回答 3

1

你没有给他们一个ID。我会给 a 标签一个带前缀的 id,以便您可以使用相同的 id 来获取相关的输入值:

<a href="#" id="q<php echo $u['question_id'];?>" // note that I added a "q" prefix

然后你应该能够像这样通过 jQuery 获得它:

var theid = $(this).attr('id'); // this being the a tag that was clicked

// then just strip off the leading "q" and you have your id.
var thehiddenid = theid.replace('q', '');
于 2013-08-06T23:49:42.833 回答
0

如果你想用纯 javascript 来做,那么从下面的事件
传递this给你的ajax_answer函数onclick

<?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
  }
?>

你的 javascript 将是......

<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>

和 jQuery 一样。
向这些输入元素添加name属性,并添加一个classnametoanchor元素

<?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
动态 添加onclick事件处理程序。现在您的函数ajax_answer接受两个参数question_idanswer我们将通过click事件处理程序传递这两个参数

<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>
于 2013-08-07T00:08:38.430 回答
0

您更改表单并添加id以便于选择:

     <form>
     <input type="hidden" value="<?php echo $u['question_id'];?>" id="question_id" />
     <input type="text"/>
     <a href="#" onClick="ajax_answer();">Send Answer</a>
     </form>

然后得到这样的值:

  question_id = $("#question_id").val();

或者,如果您的表单只有一个用于 question_id 的隐藏字段:

question_id = $("input[type=hidden]").val();

注意:请考虑使用<?php标签而不是短标签<?

于 2013-08-06T23:42:44.853 回答