![](/img/trans.png)
[英]check which button has been clicked in javascript when running a php page via ajax
[英]JS to force input button to be clicked when the page has been loaded
我有一个输入按钮,需要在页面加载时自动单击。 我有以下输入按钮代码:
<form enctype="multipart/form-data" name="cart" action="cart.php#order_statistics" method="POST">
<table id="order_statistics">
echo '<td><button type="submit" id="refreshCart" style="border: none; background: none; cursor: pointer;" name="clicked" value="Update Order"><img src="images/refresh.png" alt="Refresh Image"/></button></td>';
</table>
</form>
我尝试了其他几种方法来执行此操作,但无济于事。 当我尝试其他方式执行此操作时,我使用的脚本会自动按下按钮,但页面会不断刷新,并且会不断循环刷新。
我不知道是否可能是因为表单标签中的操作?
任何帮助,将不胜感激。
我的页面(cart.php)代码可在此处找到
将您的表格更改为
<form enctype="multipart/form-data" name="cart" action="cart.php#order_statistics" method="POST">
<table id="order_statistics">
echo '<td><button id="refreshCart" style="border: none; background: none; cursor: pointer;" name="clicked" value="Update Order"><img src="images/refresh.png" alt="Refresh Image"/></button></td>';
echo '<td><button type="submit" style="border: none; background: none; cursor: pointer;" name="clicked" value="Place order Order"><img src="images/Place order.png" alt="Place order Image"/></button></td>';
</table>
</form>
将此命令添加到脚本中。
$("#refreshCart").trigger("click");
签出trigger()函数。
使用jQuery:
$(window).load(function() {
$('#refreshCart').click();
});
或本机JavaScript:
window.onload = function() {
document.getElementById('refreshCart').click();
};
为了避免您发表评论时出现无限循环,您需要通过AJAX请求更改提交:
$(window).load(function() {
$.ajax({
url: "/your_form.php",
type: "post",
data: your_data
});
});
我相信您想要做的就是通过单击按钮提交表单,因此,我建议您以编程方式提交表单。
方法1:
document.cart.submit();
方法2:在您的表单中输入一个ID,然后
document.getElementById('giveSomeIdToForm').submit();
另外,要以编程方式单击该按钮,
document.getElementById('refreshCart').click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.