页面刷新后HTML表单数组消失

时间:2014-09-16 22:42:29

标签: php jquery arrays ajax forms

我有以下html:

<html>
<body>

<div id='refresh'>

<form id='form1' action='do.php' method='post'>
<input type='checkbox' name='thetickets[]' value='1'>
<input type='hidden' name='gender'>
<input type='submit' value='Go'>
</form>

</div>


</body>
</html>

以下JS:

<script type="text/javascript">
$(document).ready(function() {// REFRESH DIV EVERY 5 SECONDS 
  interval = setTimeout(refreshpage, 5000);
  function refreshpage() {
     $('#refresh').load('page.php?&timer='+new Date().getTime()+' #refresh');
     interval = setTimeout(refreshpage, 5000);
  }   
});
</script>

<script type="text/javascript"> /SUBMIT FORM
$(document).ready(function() { 
    var options = { 
        target:        '',
        dataType:      'html',          
        beforeSubmit:  showRequest_reassign,        
        success:       showResponse_reassign
    }; 

    $("#refresh").delegate("#form1,"submit",function () {
        clearTimeout(interval); 
        $(this).ajaxSubmit(options); 
        return false;
    });

});
function showRequest_reassign(formData, jqForm, options){
    return true; 
}
function showResponse_reassign(responseText, statusText, xhr, $form){
   alert(responseText)
}
</script>

如果我在5秒之前提交表单,则表单正确提交,但如果我等待5秒,页面刷新,我提交表单,但输入类型“thetickets []”不提交。我检查开发人员工具,表单数据,并且值“thetickets []”甚至不存在....我迷失了为什么..

谢谢,

3 个答案:

答案 0 :(得分:1)

我想在这里发布我的编辑代码,这可能有助于您找到问题所在:

<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {// REFRESH DIV EVERY 5 SECONDS 
  interval = setTimeout(refreshpage, 5000);
  function refreshpage() {
     var thisurl = document.URL;
     $('#refresh').load(thisurl + '?timer='+new Date().getTime()+' #refresh');
     interval = setTimeout(refreshpage, 5000);
     console.log("reloaded");
  }   
});
</script>

<script type="text/javascript"> //SUBMIT FORM
$(document).ready(function() { 
    var options = { 
        target:        '',
        dataType:      'html',          
        beforeSubmit:  showRequest_reassign,        
        success:       showResponse_reassign
    }; 

    $("#refresh").delegate("#form1","submit",function () {
        clearTimeout(interval); 
        $(this).ajaxSubmit(options); 
        return false;
    });

});
function showRequest_reassign(formData, jqForm, options){
    return true; 
}
function showResponse_reassign(responseText, statusText, xhr, $form){
   alert(responseText)
}
</script>

<div id='refresh'>

<form id='form1' action='http://atlantis.cit.ie/displayvalues.php' method='post'>
<input type='checkbox' name='thetickets[]' value='1'>
<input type='hidden' name='gender'>
<input type='submit' value='Go'>
</form>

</div>


</body>
</html>

如果您能向我提供有关您所犯错误的更多信息,我非常乐意为您提供帮助!

答案 1 :(得分:0)

我发现这个代码唯一的东西(除了上面提到的拼写错误)是你的“thisurl”var必须是一个php页面,它必须已经在查询字符串中有其他vars。

注意:如果您实际选中了该框,则只会获得$thetickets数组。

答案 2 :(得分:0)

发现问题,我的原始代码结构不正确。

<div>

<form>

</div>

</form>

删除了div,现在表单正常工作。