将动态生成的php变量传递给jquery函数

时间:2019-01-17 11:11:34

标签: php jquery mysql

我正在<ul>中列出问题和答案(MCQ),这些问题和答案将根据mysql结果动态显示。之后,我提供了一个注释部分,当用户单击每个问题所附的“添加注释”时,该区域会打开。我想通过为每个问题选择相应的问题ID来保存为每个问题编写的注释。我无法获取与每个问题相关的特定问题ID。根据我的代码,所有问题id都显示在console.log上。

<?php if( $row['section'] == 2) {?> 
<ul class="list-group">         
          <li class="list-group-item active">
           <div class="radio noMargin" id="<?php echo $sectionOne ; ?>info"><?php echo $no ; ?> <span class="glyphicon glyphicon-arrow-right"></span></div><?php echo "<font color='pink'>Ques ID :[".$row['id']."]</font>";?><div class="question"><?php echo $row['question'] ; ?></div>

          <div style="float:right; margin-top:-20px;">

          Marks:<?php echo $row['marks'] ; ?></div></li>
<?php          
echo '<input type="hidden" name="question[]" id="questionId" value="'.$row['id'].'">';
echo '<input type="hidden" name="attempt" value="'.$attemptCount.'">';
echo '<input type="hidden" name="unit_id" value="'.$unit_id.'">';
echo '<input type="hidden" name="chapter_id" value="'.$chapter_id.'">';

$questionId     =   $row['id'] ;  

$sqO =$db->query("SELECT id, options from tbl_options_mock_question WHERE question_id = ".$questionId."");
while($rowO=mysql_fetch_array($sqO))
{
?>  
    <li class="list-group-item wrp_radio">
      <div class="radio noMargin">
        <label>
          <input disabled type="radio" class="checkedInfo" alt="<?php echo $no ; ?>" name="optionAns<?php echo $i ; ?>" value="<?php echo $rowO['id'] ; ?>">
          <?php echo $rowO['options'] ; ?></label>
      </div>
    </li>
<?php } ?>
<br>   
 <a class="click_notes"> <font size='2' color='blue'>&nbsp;&nbsp;&nbsp;Add Notes</font></a> 
<br>
<div class="demo"></div>    
</ul>        
<?php $i++;$no++; ?>
<?php } ?>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var id = '<?php echo $row['id'] ; ?>';
console.log(id);
 $('.click_notes').on('click',function(){
  $('.comment_form').hide();
   $(this).closest('ul').find('.demo').html("<div class='comment_form'><form action='#' method='post'><textarea cols ='50' class='span10' name='notes' rows='6'></textarea><br><input class='btn btn-primary' name= 'submit_notes' type='submit' value='Add Notes'></form><br></div>");
 });
});
</script>

1 个答案:

答案 0 :(得分:0)

正如我在评论中解释的那样,将每个问题的ID都一个一个地放在标签的属性中。我对您的jQuery代码进行了以下更改。

1)我使用jQuery的$(this).data('question-id);从标记中选择问题ID。

2)然后,我添加该question_id的隐藏字段,以便您在提交表单后获得相应的问题ID。

<?php if( $row['section'] == 2) {?> 
<ul class="list-group">         
          <li class="list-group-item active">
           <div class="radio noMargin" id="<?php echo $sectionOne ; ?>info"><?php echo $no ; ?> <span class="glyphicon glyphicon-arrow-right"></span></div><?php echo "<font color='pink'>Ques ID :[".$row['id']."]</font>";?><div class="question"><?php echo $row['question'] ; ?></div>

          <div style="float:right; margin-top:-20px;">

          Marks:<?php echo $row['marks'] ; ?></div></li>
<?php          
echo '<input type="hidden" name="question[]" id="questionId" value="'.$row['id'].'">';
echo '<input type="hidden" name="attempt" value="'.$attemptCount.'">';
echo '<input type="hidden" name="unit_id" value="'.$unit_id.'">';
echo '<input type="hidden" name="chapter_id" value="'.$chapter_id.'">';

$questionId     =   $row['id'] ;  

$sqO =$db->query("SELECT id, options from tbl_options_mock_question WHERE question_id = ".$questionId."");
while($rowO=mysql_fetch_array($sqO))
{
?>  
    <li class="list-group-item wrp_radio">
      <div class="radio noMargin">
        <label>
          <input disabled type="radio" class="checkedInfo" alt="<?php echo $no ; ?>" name="optionAns<?php echo $i ; ?>" value="<?php echo $rowO['id'] ; ?>">
          <?php echo $rowO['options'] ; ?></label>
      </div>
    </li>
<?php } ?>
<br>   
 <a class="click_notes" data-question-id="<?=$rowO['id']?>"> <font size='2' color='blue'>&nbsp;&nbsp;&nbsp;Add Notes</font></a> 
<br>
<div class="demo"></div>    
</ul>        
<?php $i++;$no++; ?>
<?php } ?>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var id = '<?php echo $row['id'] ; ?>';
console.log(id);
 $('.click_notes').on('click',function(){
  var question_id =$(this).data('question-id);
  $('.comment_form').hide();
   $(this).closest('ul').find('.demo').html("<div class='comment_form'><form action='#' method='post'><input type='hidden' name='question_id' value='"+  +"'><textarea cols ='50' class='span10' name='notes' rows='6'></textarea><br><input class='btn btn-primary' name= 'submit_notes' type='submit' value='Add Notes'></form><br></div>");
 });
});
</script>