javascript 尝试用greasemonkey模拟按钮的点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5357397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to simulate the clicking of a button with greasemonkey
提问by Cody Bonney
I'm trying to write a greasemonkey script that updates inventory for a collection of items through the browser. To do this I need to autofill a few forms and simulate a mouseclick on the submission button. I have the forms filling out fine, but I'm stuck on the button submission.
我正在尝试编写一个greasemonkey 脚本,该脚本通过浏览器更新一组项目的库存。为此,我需要自动填充一些表单并模拟在提交按钮上单击鼠标。我的表格填写得很好,但我被困在按钮提交上。
Here is the HTML for the button I am trying to simulate clicking
这是我试图模拟点击的按钮的 HTML
<input type="submit" value="Find" style="" name="process-form" onclick="imhocaller.value='find-resources-page.left'; imhoaction.value='process-form';">
I tried doing something like this, but haven't had any luck.
我尝试做这样的事情,但没有任何运气。
document.getElementsByName('process-form').submit();
I can post more code if needed. Thank you!
如果需要,我可以发布更多代码。谢谢!
回答by Cody Bonney
Figured it out. needed to use .click() instead of .submit(). I also needed to add the [0] as Georgy suggested.
弄清楚了。需要使用 .click() 而不是 .submit()。我还需要按照 Georgy 的建议添加 [0]。
document.getElementsByName('process-form')[0].click();
回答by Cody Bonney
You can also dispatch a custom click event via most recent browsers and "fire" an onclick
event in IE. This is a recent feature that allows tons of control over events.
您还可以通过最新的浏览器发送自定义点击事件并onclick
在 IE 中“触发”事件。这是最近的一项功能,允许对事件进行大量控制。
var button = document.getElementById("test");
button.onclick = function()
{
alert("event was dispatched on me");
}
if(document.createEvent)
{
var click = document.createEvent("MouseEvents");
click.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
button = document.getElementById("test");
button.dispatchEvent(click);
button.focus();
}else if(document.documentElement.fireEvent)
{
button = document.getElementById("test");
button.fireEvent("onclick");
button.focus();
}
Do note that you need to listen for the event before dispatching it.
请注意,您需要在调度事件之前侦听该事件。
回答by giacoder
try this document.getElementsByName('process-form')[0].submit();
试试这个 document.getElementsByName('process-form') [0].submit();
UPD: Right! Must be .click() and not .submit()
UPD:对!必须是 .click() 而不是 .submit()
回答by mravey
Here you try to submit a input, you have to submit the form. So if you tag as an id of #myForm you need to do : document.getElementById('myForm').submit()
在这里你尝试提交一个输入,你必须提交表单。因此,如果您标记为 #myForm 的 id,您需要执行以下操作:document.getElementById('myForm').submit()
回答by Haoyu Chen
Another way to do it is to use .trigger( "submit" )
in JQuery.
另一种方法是.trigger( "submit" )
在 JQuery 中使用。