我有以下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[]"根本不存在。。。。我不知道为什么。。
谢谢,
我想在这里发布你的代码和我的编辑,这可能会帮助你找到你的问题:
<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>
如果你能向我提供更多关于你所得到的错误的信息,我将非常乐意提供帮助!
我在这段代码中发现的唯一一件事(除了上面提到的打字错误)是,你的"thisurl"var必须是一个php页面,并且它在查询字符串中必须已经有其他var。
注意:只有在实际选中该框的情况下,才能获得$thetickets
数组。
发现问题,我的原始代码结构不正确。
<div>
<form>
</div>
</form>
删除了div,现在表单可以正常工作了。