如何使用JavaScript提交表单 [英] How to submit a form using javascript

查看:125
本文介绍了如何使用JavaScript提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单击提交"按钮,并使用纯JavaScript提交表单.我已经尝试过:

I'm trying to click on a submit button and submit a form using purely javascript. I have tried:

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

for(var i=0;i<document.getElementsByName('operation').length;i++){
    if(document.getElementsByName('operation')[i].value=='Sell'){
        document.getElementsByName('operation')[i].submit();
    }
}

但我无法理解.谁能告诉我我该怎么做?

but I just can't get it. Can anyone show me how I could do this?

谢谢.

Web代码是:

<form id="sell_form" class="ajaxform" action="/member/sell" method="post">
    <div id="result" class="popup"> </div>  //hidden
    <div class="c-box-body">
        <table width="100%" border="0">
        <div style="text-align:center">
            <br>
            <input type="hidden" value="13" name="pair">    //hidden
            <input type="hidden" value="Sell" name="type">  //hidden
            <input type="submit" value="Calculate" name="calculate">
            <input type="submit" value="Sell" name="operation">   //**This is the one I'm trying to click on
            <input type="reset" value="Clear">
        </div>
    </div>
</form>

造成这一困难的原因是该网页不是我的(我在firefox中使用的是oilmonkey,本质上是onload javascript事件)

What makes this difficult is that the web page is not mine (I'm using greasemonkey in firefox which is essentially and onload javascript event)

推荐答案

我能够通过以下方式实现此目标:

I was able to to it with:

var element = document.querySelector('#sell_form input[name="operation"]');  
    if (element) {  
        element.click();
    }

这篇关于如何使用JavaScript提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆