Submit an HTML form without having a submit button?

前端 未结 11 1440
攒了一身酷
攒了一身酷 2021-01-01 18:57

Can I submit a html

with
instead of ?

Like this:

         


        
相关标签:
11条回答
  • 2021-01-01 19:24

    The method you can use to submit a specific form is the following:

    // Grab the form element and manually trigger the 'submit' method on it:
    document.getElementById("myForm").submit();
    

    So in your example, you can add a click event handler on any element you like and trigger the form's submit method through that:

    <form method="post" id="myForm">
      <textarea name="reply">text</textarea>
    </form>
    
    <div class="submit">Submit the form by clicking this</div>
    
    const myForm = document.getElementById("myForm");
    document.querySelector(".submit").addEventListener("click", function(){
    
      myForm.submit();
    
    });
    

    And if you want to do it jQuery style (which I do not recommend for such a simple task);

    $("#myForm").submit();
    

    In its full form:

    const myForm = $("#myForm");
    $(".submit").click(function(){
    
      myForm.submit();
    
    });
    

    References:

    • The submit() method of HTML Form elements (native JavaScript API)
    • The jQuery submit() API
    0 讨论(0)
  • 2021-01-01 19:25
    <?php
    if(isset($_POST['text']) && !empty($_POST['text']))
    {
       echo $text_val = $_POST['text'];
    }
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
            <form method="post" action="Enter your action">
            <p><label>Enter your Text : </label>
            <input type="text" name="text" id="text" onmouseover="this.form.submit();"></input>
            </p>
            </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-01 19:30

    Using jquery:

    $('#myForm').submit();
    
    0 讨论(0)
  • 2021-01-01 19:34

    Yes, it's fairly simple. Just use the submit[jQuery docs] method inside of a click handler function.

    $("#myDiv").click(function() {
     $("#myForm").submit();
    });
    

    If you prefer, you can do it with vanilla Javascript:

    document.getElementById("myDiv").onclick = function() {
      document.getElementById("myForm").submit();
    };
    
    0 讨论(0)
  • 2021-01-01 19:39

    How about this:

     $(document).ready(function() {
         $('#submitDiv').click(function() {
            $('#myForm').submit();
         });
    
    }); 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form method="post" action="" id="myForm">
    <textarea name="reply">text</textarea>
    </form>
    
    <div id="submitDiv">Submit the form by clicking this</div>

    0 讨论(0)
  • 2021-01-01 19:42

    If you want to unnecessarily depend on JavaScript, then you could…

    jQuery('div').click(function () { jQuery('form').submit(); });
    

    … however, you should use semantic HTML that works without JS being present. So use a real submit button and apply CSS to make it look the way you want.

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