如何使用javascript提交表单

时间:2013-12-06 05:26:43

标签: javascript forms submit form-submit

我正在尝试点击提交按钮并使用纯粹的javascript提交表单。我试过了:

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

但我无法得到它。任何人都可以告诉我如何做到这一点吗?

提前致谢。

网络代码是:

<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中使用的是greasemonkey,这本质上是onload javascript事件)

3 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
  function callSubmit() {      
      document.forms[0].submit();
  }
</script>

<form id="sell_form" class="ajaxform" action="/member/sell" method="post">
    <div id="result" class="popup"> </div>  //hidden`enter code here`
    <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" onclick="callSubmit()">
            <input type="reset" value="Clear">
        </div>
    </div>
</form>

答案 1 :(得分:0)

var form = document.getElementById("sell_form"),
    button = document.getElementById("sell_button");

function submitForm() {
    form.submit();
}

button.addEventListener("click", submitForm, false);

我正在使用DOM获取表单和按钮实例。之后,我创建了一个提交表单的函数。最后,我告诉浏览器“监听”按钮元素,当它“听到”点击它时,它应该运行以前创建的函数。

答案 2 :(得分:0)

我能够:

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