Using jQuery to submit form from link outside of form

后端 未结 4 1517
攒了一身酷
攒了一身酷 2021-02-10 10:38

I\'m trying to submit a form from a link that is located in my nav bar.

I\'m new to javascript.jQuery and unsure why this won\'t execute.

I have a feeling there

相关标签:
4条回答
  • 2021-02-10 11:04

    You are clicking in the "A", not in the Italic (With ID). Supposing you will not change the HTML to that work out you should use this:

    $(document).ready(function() {  
        $('#add-product-save-link').parent().click(function(e) {
            $('#add-product-form').submit();
            return false;
        });
    });
    
    0 讨论(0)
  • 2021-02-10 11:05

    $("#button").click(function(e){

            $.post('url.php', $("#form").serialize(), function(data){
    
    0 讨论(0)
  • 2021-02-10 11:11

    Link:

    <a href="#" id="add-product-save-link" class="icon-plus" ><i>Save</i></a>
    

    jQuery:

    $(document).ready(function() { 
        $('#add-product-save-link').click(function() {
            $('#add-product-form').submit();
        });
    });
    

    You don't need the e.preventDefault(), because your anchor tag isn't going anywhere (the href is #).

    Because your <form> tag's action attribute is blank, that means that the form will submit itself to this same document. If that is what you want to do, then you can put the logic for that at the top of your document:

    <?php
        if (isset($_POST)) {
            //do your form processing here
        }else{
            //your normal page is here.
        }
    ?>
    

    Finally, because this answer uses jQuery, ensure that you reference the jQuery library somewhere, such as in the <head> tags:

    <html>
        <head>
            <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        </head>
    
    0 讨论(0)
  • 2021-02-10 11:14

    in pure JavaScript: (http://jsfiddle.net/4enf7/)

    <a href="javascript:document.getElementById('add-product-form').submit();">Send form</a>
    
    0 讨论(0)
提交回复
热议问题