最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trying to simulate the clicking of a button with greasemonkey - Stack Overflow

programmeradmin2浏览0评论

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.

Here is the HTML for the button I am trying to simulate clicking

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

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.

Here is the HTML for the button I am trying to simulate clicking

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

Share Improve this question asked Mar 18, 2011 at 20:17 Cody BonneyCody Bonney 1,6581 gold badge10 silver badges17 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 8

Figured it out. needed to use .click() instead of .submit(). I also needed to add the [0] as Georgy suggested.

document.getElementsByName('process-form')[0].click();

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.

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();
}

http://jsfiddle.net/ZWyp7/3/

Do note that you need to listen for the event before dispatching it.

try this document.getElementsByName('process-form')[0].submit();

UPD: Right! Must be .click() and not .submit()

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

Another way to do it is to use .trigger( "submit" ) in JQuery.

发布评论

评论列表(0)

  1. 暂无评论