Using jQuery to submit form from link outside of form

后端 未结 4 1520
攒了一身酷
攒了一身酷 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:11

    Link:

    Save
    

    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

    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:

    
    

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

    
        
            
        
    

提交回复
热议问题