简体   繁体   English

如何自动提交JavaScript表格

[英]How to auto submit javascript form

There is a search button aa webpage : 网页上有一个搜索按钮:

<td><table border="0" cellpadding="0" cellspacing="0"><tr><td dir="ltr" width="10" height="21"><img src="/global/images/ButtonLeftDove.gif" border="0" alt="" height="21" width="10" /></td><td height="21" align="center" valign="middle" class="ButtonDove" nowrap="nowrap"><a href="javascript:__doPostBack('mobjTemplate$ctl01$btnSearch2','')" onmouseover="window.status='Search';return true;" onmouseout="window.status='';return true;">Search</a></td><td dir="ltr" width="10" height="21"><img src="/global/images/ButtonRightDove.gif" border="0" alt="" height="21" width="10" /></td></tr></table></td>

it is calling a javascript function javascript:__doPostBack('mobjTemplate$ctl01$btnSearch2','') 它正在调用javascript函数javascript:__ doPostBack('mobjTemplate $ ctl01 $ btnSearch2','')

the java function is as following: java函数如下:

function __doPostBack(eventTarget, eventArgument) {

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

        theForm.__EVENTTARGET.value = eventTarget;

        theForm.__EVENTARGUMENT.value = eventArgument;

        theForm.submit();

    }

}

I need to auto click that search button. 我需要自动单击该搜索按钮。

I tried this code but it does not work: 我尝试了这段代码,但是它不起作用:

HtmlElementCollection elemColl2 = null;
HtmlDocument doc = webBrowser.Document;
elemColl2 = doc.GetElementsByTagName("form");

foreach (HtmlElement elem in elemColl2)
            {
                elem.InvokeMember("submit");
            }

How can I do that?? 我怎样才能做到这一点??

Thanks for help in advance.. 预先感谢您的帮助。

I would suggest trying to get the anchor (or button) and use .click() on that. 我建议尝试获取锚点(或按钮)并在其上使用.click()。 I've just had poor luck using 'submit' on the form itself in javascript in the past (which is what you're doing, through a layer of indirection). 过去,我只是在javascript中在表单本身上使用“提交”而感到不走运(这是您通过间接层所做的事情)。

You can use: 您可以使用:

link.InvokeMember("Click");

To click the link once you've gotten it into a variable. 将链接放入变量后,单击该链接。

I suspect the reason that it's not "submitting" is because the element in the page isn't a form element and doesn't have an associated submit action. 我怀疑它不是“提交”的原因是因为页面中的元素不是表单元素,并且没有关联的提交动作。 It's an anchor element with some JavaScript in the href (which is distasteful to say the least, but it's not your code so don't worry). 这是一个带有href某些JavaScript的锚元素(至少可以说这令人反感,但这不是您的代码,所以请不必担心)。

(Side note, you keep saying "Java" where you mean "JavaScript." The two are quite different.) (旁注,您一直说“ Java”,意思是“ JavaScript。”两者是完全不同的。)

This answer to a very similar (though not exactly duplicate, so I'm hesitant to vote to close this question) question appears to be a workable solution. 这个答案非常相似(尽管不是完全重复,所以我犹豫不决要关闭这个问题),这似乎是一个可行的解决方案。 You may need to add a COM reference, which I personally try to avoid unless absolutely necessary (but that's just me), but being able to invoke a "click" on the anchor element in question should do the trick. 您可能需要添加一个COM引用,我个人尽量避免,除非绝对必要(不过这只是我),但能够调用“点击”的问题应该做的伎俩锚元素。 (Assuming the WebBrowser control is interpreting the JavaScript on the page correctly.) (假设WebBrowser控件正确解释了页面上的JavaScript。)

You can get the button element with javascript and then call the click event manually. 您可以使用javascript获取button元素,然后手动调用click事件。 And... I will rather stop programming like that (I mean calling asp.net ids in javascript. There's so much easier ways to do the same. Eg: a submit button 而且...我宁愿停止那样的编程(我的意思是在javascript中调用asp.net id。这样​​做的方法非常简单。例如:提交按钮

Understand the fact that the code you write in C# is going to be executed at the server side. 了解使用C#编写的代码将在服务器端执行的事实。 What you actually need is to write a javascript code so that that form get submitted at client side. 您真正需要的是编写一个JavaScript代码,以便该表单在客户端提交。 you can call form.submit when you want to submit the form. 您可以在提交表单时致电form.submit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM