jQuery - 在提交时向表单添加隐藏输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8231946/
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-27 10:13:56  来源:igfitidea点击:

jQuery - Adding hidden input to form on submit

jquery

提问by oprogfrogo

I'm looking for a way to insert a hidden input on submitting for 'Yes' in my jquery code below.

我正在寻找一种在下面的 jquery 代码中提交“是”时插入隐藏输入的方法。

How do I insert this:

我如何插入这个:

<input type="hidden" name="token" value="1">

From this:

由此:

$(function() {
    $( "#dialog" ).dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            "Yes": function() {
                $( '#form' ).submit();
            },
            "No": function() {
                $( '#form' ).submit();
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
});

回答by ShankarSangoli

Try this

尝试这个

$(function() {
    $( "#dialog" ).dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            "Yes": function() {
                $('#form').append('<input type="hidden" name="token" value="1" />').submit();
            },
            "No": function() {
                $('#form').submit();
            },
            Cancel: function() {
                $(this).dialog( "close" );
            }
        }
    });
});

回答by Johan

Easy answer, first adding the input and then calling submit (I'm guessing a bit of context btw).

简单的回答,首先添加输入,然后调用提交(顺便说一句,我在猜测一些上下文)。

$('#form').append('<input type="hidden" name="token" value="1">').submit();

回答by Peter Kellner

I'm new to JQuery but thought others might appreciate this for adding multiple values.

我是 JQuery 的新手,但认为其他人可能会因为添加多个值而欣赏这一点。

var myFormData = [
    {
        name: 'x_login',
        value: 'xxxxxxxxxx'
    }, {
        name: 'x_amount',
        value: donationAmount
    }
];

$.each(myFormData, function (index, myData) {
    debugger;
    $('#registerformid').append('<input type="hidden" name=' + myData.name + ' value=' + myData.value + '>');
});

$('#registerformid').submit();