我只是想知道这段代码哪里出了问题--它将in 95添加到结帐中,但问题是,它反复执行,就像循环中的那样,我似乎无法理解如何阻止它。
<body onLoad="submit1()">
<form method="post" action="" class="jcart" id="foo">
<fieldset>
<input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
<input type="hidden" name="my-item-id" value="ABC-8" />
<input type="hidden" name="my-item-name" value="Level 1" />
<input type="hidden" name="my-item-price" value="95.00" />
<input type="hidden" name="my-item-url" value="" />
<table>
<tr>
<td width="65%">
<strong>Level 1 all for £95</strong>
</td>
<td width="15%" align="right">
£95.00
</td>
<td>
<input type="text" hidden ="true" name="my-item-qty" value="1" size="3" hidden="true" />
</td>
<td width="20%" align="center">
<input type="submit" name="my-add-button" id="my-add-button" value="add" class="button" />
</td>
</tr>
</table>
</fieldset>
</form>
<script type="text/javascript">
function submit1() {
// It seems that its this causing the problem
document.getElementById("my-add-button").click()// Simulates button click
document.foo.submit() // Submits the form without the button
}
</script>
</body>发布于 2014-05-29 10:16:19
因为在body onload中调用submit1(),所以每次加载页面时都会执行它。由于此函数提交再次加载页面的表单,因此它创建了一个循环。删除submit1()函数并尝试这种更简单的方法:
$("#my-add-button").click(function() {
$("#foo").submit();
});发布于 2014-05-29 10:19:23
试试这个:
var o = 0;
function submit1() {
if(o === 0){
document.foo.submit() // Submits the form without the button
}
(o === 0) ? o++ : o = 0;
} https://stackoverflow.com/questions/23930883
复制相似问题