这是我的代码
查看页面"问题": -
<input type="hidden" id="h3" value="<?php echo $t;?>">
<script>
var seconds =document.getElementById('h3').value;
function a()
{
var minutes = Math.round((seconds - 30)/60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10)
{
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
if (seconds == 0)
{
clearInterval(countdownTimer);
alert('Time Out');
window.location.href="result.php";
}
else
{
seconds--;
}
}
var countdownTimer = setInterval('a()', 1000);
function b()
{
window.location.href="result.php";
}
</script>
<script src="jquery.js"></script>
<script type="text/javascript">
// Ajax post
$(document).ready(function() {
$('#f input').on('change', function() {
var ans=$('input[name="r"]:checked', '#f').val();
});
$(".submit").click(function() {
jQuery.ajax({
type: "POST",
url: "http://localhost/ci/index.php/controller_q/quchng",
data: {ans:'ans'},
success: function() {
$('#i').innerHTML();
}
});
});
});
</script>
<div id="i">
<?php $a=1; ?>
Time left- <span id="countdown" class="timer"></span>
Q: <?php echo $qno['ques'.$a];?>
<form id="f">
<input type="radio" name="r5" id="r1" value="<?php echo $qno['a'.$a];?>"><?php echo $qno['a'.$a];?>
<br>
<input type="radio" name="r5" id="r2" value="<?php echo $qno['b'.$a];?>"><?php echo $qno['b'.$a];?>
<br>
<input type="radio" name="r5" id="r3" value="<?php echo $qno['c'.$a];?>"><?php echo $qno['c'.$a];?>
<br>
<input type="radio" name="r5" id="r4" value="<?php echo $qno['d'.$a];?>"><?php echo $qno['d'.$a];?>
<br>
<button id="b" class="submit" value='+'>Save & Next</button>
<input type="hidden" id="h" value="<?php print_r($qno);?>">
<input type="hidden" id="h2" value="<?php echo $a;?>">
</form>
</div>
这是我的控制器页面: -
function fexam()
{
$a=$this->input->get();
$d=$a['val'];
$this->load->model('model_q');
$no=$this->model_q->checktest($d);
if($no==1)
{
$t=$this->model_q->time();
$m=$this->model_q->qno();
$no=$this->model_q->selectques($d,$m);
$data=array('t'=>$t,'qno'=>$no);
$this->load->view('ques',$data);
}
else
{
}
}
function quchng()
{
$ans=$this->input->post('ans');
echo $ans;
}
但是当我点击按钮时,它会将我重定向到控制器功能fexam()
,该功能是给定的网址链接&#34; quchng&#34;。请告诉我需要编辑的地方来解决这个问题。
答案 0 :(得分:0)
编辑: 同时更新动态事件的点击功能或将此脚本移到页面底部,它肯定会起作用:
$(document).on("click",".submit",function() {
alert("Worked")
jQuery.ajax({
type: "POST",
url: "http://localhost/ci/index.php/controller_q/quchng",
data: {ans:'ans'},
success: function() {
$('#i').innerHTML();
}
});
});
更新您的按钮以输入按钮,因为它将其作为&#34;提交&#34;默认情况下:
<button type="button" id="b" class="submit" value='+'>Save & Next</button>
希望它有效
答案 1 :(得分:0)
您可以使用:
<form id="f" onsubmit="return false;">
data: {ans:'ans'}
应该像
data: {ans:ans},