您好,我在这里有一段代码,它从数据库中获取数据(问题和选项)。我需要帮助的是,我想在单击“提交”按钮
时获得所有答案。 <?php
$res = $conn->query("select * from questions where category_id='$category' ORDER BY RAND()") ;
$rows = mysqli_num_rows($res);
$i=1;
while($result=mysqli_fetch_array($res)){?>
<?php if($i==1){?>
<div id='question<?php echo $i;?>' class='cont'>
<p class='questions' id="qname<?php echo $i;?>"> <?php echo $i?>.<?php echo $result['question_name'];?></p>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer1'];?>
<br/>
<input type="radio" value="2" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer2'];?>
<br/>
<input type="radio" value="3" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer3'];?>
<br/>
<input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer4'];?>
<br/>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
<button id='next<?php echo $i;?>' class='next btn btn-success' type='button'>Next</button>
</div>
<?php }elseif($i<1 || $i<$rows){?>
<div id='question<?php echo $i;?>' class='cont'>
<p class='questions' id="qname<?php echo $i;?>"><?php echo $i?>.<?php echo $result['question_name'];?></p>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer1'];?>
<br/>
<input type="radio" value="2" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer2'];?>
<br/>
<input type="radio" value="3" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer3'];?>
<br/>
<input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer4'];?>
<br/>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
<button id='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button>
<button id='next<?php echo $i;?>' class='next btn btn-success' type='button' >Next</button>
</div>
<?php }elseif($i==$rows){?>
<div id='question<?php echo $i;?>' class='cont'>
<p class='questions' id="qname<?php echo $i;?>"><?php echo $i?>.<?php echo $result['question_name'];?></p>
<input type="radio" value="1" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer1'];?>
<br/>
<input type="radio" value="2" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer2'];?>
<br/>
<input type="radio" value="3" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer3'];?>
<br/>
<input type="radio" value="4" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/><?php echo $result['answer4'];?>
<br/>
<input type="radio" checked='checked' style='display:none' value="5" id='radio1_<?php echo $result['id'];?>' name='<?php echo $result['id'];?>'/>
<br/>
<button id='pre<?php echo $i;?>' class='previous btn btn-success' type='button'>Previous</button>
<button id='next<?php echo $i;?>' class='next btn btn-success submit' type='submit'>Finish</button>
</div>
<?php
}
$i++;
} ?>
</form>
请为我提供一个jquery代码,该代码可以在提交时获取所有答案。 这就是我在下面尝试过的
$(document).on('click','.submit',function(){
var answer = [];
$. each($(this.checked), function(){
answers. push($(this). val());
});
alert("your answers are: " + answers. join(", "));
答案 0 :(得分:0)
尝试一下:
CREATE TABLE IF NOT EXISTS client
(
pk_macaddr VARCHAR(17) PRIMARY KEY NOT NULL,
ipaddress VARCHAR(15) NOT NULL ,
hostname VARCHAR(50) UNIQUE NOT NULL ,
fk_pk_roomnumber INTEGER NOT NULL ,
last_seen DATE ,
is_online BOOLEAN default false
);
这部分
$('form').on('submit',function(){
var answer = [];
$(this).find('input[type="radio"]:checked').each(function(){
answers.push($(this).val());
});
alert("your answers are: " + answers.join(", "));
//return false to prevent submission
});
毫无意义, $(this.checked)
在此上下文中是this
按钮。因此,这并不能告诉我们如何找到复选框/单选按钮。
在jQuery中,无论如何我们都可以将.submit
用于复选框/单选按钮,以便您可以执行上面的操作,例如:checked
。
相反,我们应该将表单的Submit操作用作事件,因为if($(foo).is(':checked'))
是包含复选框/单选按钮的表单。这样可以更轻松地找到它们。您可以使用按钮的click事件,但随后需要执行以下操作:
this
因为 //instead of $(this).find('input[type="radio"]:checked').each( ... )
$('.submit').click(function(){
$(this).closest('form').find('input[type="radio"]:checked').each( ... );
//...
});
是按钮,所以我们必须获取父表单,然后获取复选框/单选按钮。