【问题标题】:JS to force input button to be clicked when the page has been loadedJS在页面加载后强制点击输入按钮
【发布时间】:2017-04-13 12:39:44
【问题描述】:

我有一个输入按钮,需要在页面加载时自动单击。我有以下输入按钮代码:

<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>

我尝试了其他几种方法,但无济于事。当我尝试其他方式时,我使用的脚本会自动按下按钮,但页面会不断刷新,并且会不断循环刷新。

不知道是不是因为form标签里面的action?

任何帮助将不胜感激。

我的页面(cart.php)代码可以在here找到

【问题讨论】:

  • 您是否尝试过在 $(document).ready 上插入函数?这样当用户最终进入该表单时,它会自动触发该功能?

标签: javascript php jquery forms refresh


【解决方案1】:

把你的表格改成

<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() 函数。

【讨论】:

  • 您好,感谢您的评论。我已经尝试过了,但是页面最终会在无限循环中一遍又一遍地刷新自己。你想让我为你发布我的整个页面吗?
  • 从您的按钮中删除 type="submit",它将您的表单作为提交按钮提交。
  • 我刚试过这个,它仍然在无限循环中不断刷新。如果您愿意,我可以发布代码吗?
【解决方案2】:

使用 jQuery:

$(window).load(function() {
    $('#refreshCart').click();
});


或原生 JavaScript:

window.onload = function() {
  document.getElementById('refreshCart').click();
};


为避免无限循环,您是否评论过,您需要将提交更改为AJAX request

$(window).load(function() {
    $.ajax({
        url: "/your_form.php",
        type: "post",
        data: your_data
    });
});

【讨论】:

  • 您好,感谢您的评论。使用您的代码时,我遇到了与尝试 Vikram Sangat 下面发布的代码时相同的问题。该页面最终会在无限循环中一遍又一遍地刷新自己。你想让我为你发布我的整个页面吗?
【解决方案3】:

我相信您想要做的是通过单击按钮提交表单, 所以我建议你以编程方式提交表单。

方法1:

document.cart.submit();

方法2:给你的表单一个ID,然后

document.getElementById('giveSomeIdToForm').submit();

另外,以编程方式单击按钮,

document.getElementById('refreshCart').click();

【讨论】:

  • 您好,感谢您的评论。然而,这具有与上述相同的问题。购物车只是在一个无限循环中不断刷新。要我粘贴整个页面的代码吗?
  • 可能是因为。您插入此代码的页面恰好是 cart.php。因此,每当您提交表单时,它往往会点击 action 属性中的 url,这将再次以编程方式提交表单,这会导致无限循环。我说的对吗?
  • 是的,我认为这是正确的。表单的“Action”是同一个页面,即 cart.php
  • 是的。所以也许你想做的是。保持 action 属性指向一个不同的 php 来执行逻辑。或者如果可能,在提交表单时在 URL 中添加一个额外的参数。如果我知道我们正在执行此自动提交的确切原因,我只能评论所有这些
  • 发生的情况是,当我将产品添加到购物车时,除非单击提交按钮,否则购物车不会加载产品。我们正在努力寻找解决方案或绕过它的方法。请在此处查看我的整个页面代码:pastebin.com/PeCp6mdG
猜你喜欢
  • 2013-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多