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

javascript - Simulate button click in a Webpage - Stack Overflow

programmeradmin1浏览0评论

i'm trying to code an application for Android that fills up a form in a webpage,submits it and parse results to show them. I'm using javascript to fill up the fields and then call the form's action, but it doesn´t work. I have studied the web html code, but i believe it uses JSF to display and handle the form. Is there any way to simulate the submit button click just as if i press it physically? In case anyone want to take a peak in the code, web url is . The fact is i'm not a big expert in web programming and i'm a bit lost :).Thx and sorry about my english

EDIT: I've also tried this: javascript:var elementToGet = "frmBusqueda:j_id29"; var form = document.forms["frmBusqueda"]; var button = form.elements[elementToGet]; button.click();

But it keeps reloading the web, not giving me the submit result

i'm trying to code an application for Android that fills up a form in a webpage,submits it and parse results to show them. I'm using javascript to fill up the fields and then call the form's action, but it doesn´t work. I have studied the web html code, but i believe it uses JSF to display and handle the form. Is there any way to simulate the submit button click just as if i press it physically? In case anyone want to take a peak in the code, web url is http://www.transportedecantabria.es. The fact is i'm not a big expert in web programming and i'm a bit lost :).Thx and sorry about my english

EDIT: I've also tried this: javascript:var elementToGet = "frmBusqueda:j_id29"; var form = document.forms["frmBusqueda"]; var button = form.elements[elementToGet]; button.click();

But it keeps reloading the web, not giving me the submit result

Share Improve this question edited Jan 18, 2011 at 14:06 Pikoh asked Jan 18, 2011 at 12:03 PikohPikoh 7,71331 silver badges56 bronze badges 2
  • The code on that page is very long and hard to follow. It's much better to write a minimal testcase and share that. sscce – Stewart Commented Jan 18, 2011 at 12:15
  • Stewart, the fact is that i don't own the page. It's a goverment web and i just want to be able to use it from my android app. Thx for caring anyway – Pikoh Commented Jan 18, 2011 at 12:26
Add a ment  | 

3 Answers 3

Reset to default 3

You should be able to call the click() method on the button object to fire it, though it would probably be better to call the submit() method on the form object.

This code made the trick:

javascript:var elementToGet = "frmBusqueda:j_id29"; var form = document.forms["frmBusqueda"]; var button = form.elements[elementToGet]; button.click();

Thx all for your help

You can use

document.forms

to retrieve the form elements within the current document.

If you want to submit the first form:

var firstForm = document.forms[0];
firstForm.submit();
发布评论

评论列表(0)

  1. 暂无评论