![](/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.