jQuery `submit` method ignores POST sends with GET. Why?

前端 未结 1 578
既然无缘
既然无缘 2021-01-19 16:19

I have a form like this.

相关标签:
1条回答
  • 2021-01-19 16:59

    As discussed in comments, there is no bug, at least on the last version of jQuery 2.0.3, one can test with this:

    <html>
    <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script>
    $(document).ready(function () {
      $('#dosubmit').on('click',function(){
        $("#hiddenForm").submit();
      });
    });
    </script>
    </head>
    <body>
    
    <?php
    //
    if(count($_GET) > 0) {
      echo "<textarea style=\"width: 700px; height: 90px;\">
    Submitted with method=GET: \$_GET:
    ";
      var_dump($_GET);
      echo "</textarea>";
    }
    
    if(count($_POST) > 0) {
    echo "<textarea style=\"width: 700px; height: 90px;\">
    Submitted with method=POST: \$_POST:
    ";
      var_dump($_POST);
      echo "</textarea>";
    }
    ?>
    
    <form id="hiddenForm" method="post" enctype="multipart/form-data">
      <input type="hidden" id="HiddenFormInput" name="HiddenFormInput" />
    </form>
    
    <div id="dosubmit" style="background-color: gold; width: 200px; cursor: pointer;">Click me to Do jQuery Submit</div>
    
    </body>
    </html>
    

    When one clicks on gold background text, $("#hiddenForm").submit(); is used to submit the form.

    Here is the output:

    Submitted with method=POST: $_POST:
    array(1) {
      ["HiddenFormInput"]=>
        string(0) ""
    }
    

    In your original form, no URL arguments are passed by the submitting, as only inputs with name attribute specified are submitted. So I added the attribute name="HiddenFormInput" for your hidden input.

    0 讨论(0)
提交回复
热议问题