How to submit a form using javascript - Stack Overflow

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

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

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

and

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?

Thanks in advance.

The web code is:

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

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'm trying to click on a submit button and submit a form using purely javascript. I have tried:

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

and

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?

Thanks in advance.

The web code is:

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

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)

Share Improve this question edited Dec 6, 2013 at 6:19 excelVBAmaster. asked Dec 6, 2013 at 5:26 excelVBAmaster.excelVBAmaster. 1581 gold badge2 silver badges13 bronze badges 4
  • Can you post a demo to reproduce the issue? – elclanrs Commented Dec 6, 2013 at 5:28
  • I'm not sure how to post a demo. The submit options that I tried simply produce the action of going to crypto-trade./member/sell but nothing gets traded (posted) and the result popup doesn't e up. – excelVBAmaster. Commented Dec 6, 2013 at 6:31
  • Try jsfiddle for a demo. – elclanrs Commented Dec 6, 2013 at 6:36
  • It just said error please use post request – excelVBAmaster. Commented Dec 6, 2013 at 6:41
Add a ment  | 

3 Answers 3

Reset to default 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>
var form = document.getElementById("sell_form"),
    button = document.getElementById("sell_button");

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

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

I'm getting the form and the button instances using the DOM. After that, I create a function which submits the form. Finally, I tell the browser to 'listen' to the button element, and when it 'hears' a click on it, it should run the previously created function.

I was able to to it with:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745493827a4630107.html

相关推荐

  • How to submit a form using javascript - Stack Overflow

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

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信