使用 jQuery 从表单外的链接提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18406732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:33:20  来源:igfitidea点击:

Using jQuery to submit form from link outside of form

javascriptjqueryformshyperlink

提问by bbrooke

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.

我是 javascript.jQuery 的新手,不确定为什么这不会执行。

I have a feeling there I need to put something else in the link's href but I'm not sure what it is? I appreciate the feedback and expertise.

我有一种感觉,我需要在链接的 href 中添加其他内容,但我不确定它是什么?我感谢反馈和专业知识。

LINK

关联

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

FORM

形式

<form class="form" action="" method="post" id="add-product-form">

JAVASCRIPT (this code in an external separate file I have linked to the template)

JAVASCRIPT(此代码位于我已链接到模板的外部单独文件中)

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

I've linked to jQuery with:

我已经链接到 jQuery:

回答by ImmortalPC

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

在纯 JavaScript 中:(http://jsfiddle.net/4enf7/

<a href="javascript:document.getElementById('add-product-form').submit();">Send form</a>

回答by cssyphus

Link:

关联:

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

jQuery:

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 #).

您不需要 e.preventDefault(),因为您的锚标记不会去任何地方(href 是#)。

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:

因为你的<form>标签的 action 属性是空白的,这意味着表单将自己提交给同一个文档。如果这是您想要做的,那么您可以将其逻辑放在文档的顶部:

<?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:

最后,因为这个答案使用 jQuery,请确保您在某处引用了 jQuery 库,例如在<head>标签中:

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>

回答by Roger Barreto

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:

您单击的是“A”,而不是斜体(带 ID)。假设您不会将 HTML 更改为您应该使用的结果:

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

回答by Ralph Alex

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

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

        $.post('url.php', $("#form").serialize(), function(data){