0

I hope to submit a form when I click the span id="ToolBarDownload" but it seems that the form id="myformID" is not submitted. What's wrong? Does my code make sense? Thanks!

By the way, the text id="ToolBarDownload" is outside the form.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="js/jquery1.10.2.min.js?isassets=1" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#ToolBarDownload").click(function() {
                if (confirm('Do you want to Download')) {
                    $("#myformID").commit();
                }
            });

            $("#myformID").submit(function(eventObj) {
                $('<input />').attr('type', 'hidden').attr('name', "myCw").attr('value', "mydata").appendTo('#myformID');
                return true;
            });
        });
    </script>
</head>
<body>
    <div id="container">
        <form action='' method='post' enctype='multipart/form-data' id="myformID"></form>
        <span id="ToolBarDownload" class="ToolBarButton" style="margin-left:5px">Download</span>
    </div>
</body>
</html>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

1

There is no commit() method in jQuery. To submit a form, use submit():

$("#ToolBarDownload").click(function(){
    if (confirm('Do you want to Download')) {
        $("#myformID").submit();
    }
});

Also note that in the submit() handler you can provide an object to the attr() method to make only a single call that changes multiple attributes. return true is also redundant and can be removed:

$("#myformID").submit(function() {
    $('<input />').attr({
        type: 'hidden',
        name: 'myCw',
        value: 'mydata'
    }).appendTo('#myformID');
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Thank you very much, could you have a look at http://stackoverflow.com/questions/40623211/how-to-retrieve-the-array-in-server-side-using-nanohttpd-in-android-when-i-post – HelloCW Nov 16 '16 at 08:13
  • Glad to help. I can't help with you other problem though. – Rory McCrossan Nov 16 '16 at 08:14