jQuery - 在提交时添加其他参数(不是 ajax)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2530635/
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
jQuery - add additional parameters on submit (NOT ajax)
提问by Ciel
Using jQuery's 'submit' - is there a way to pass additional parameters to a form? I am NOT looking to do this with Ajax - this is normal, refresh-typical form submission.
使用 jQuery 的“提交” - 有没有办法将附加参数传递给表单?我不想用 Ajax 来做这件事——这是正常的、典型的刷新表单提交。
$('#submit').click(function () {
$('#event').submit(function () {
data: {
form['attendees'] = $('#attendance').sortable('toArray').toString();
});
});
回答by Michel
This one did it for me:
这个是为我做的:
var input = $("<input>")
.attr("type", "hidden")
.attr("name", "mydata").val("bla");
$('#form1').append(input);
is based on the Daff's answer, but added the NAME attribute to let it show in the form collection and changed VALUE to VAL Also checked the ID of the FORM (form1 in my case)
基于 Daff 的答案,但添加了 NAME 属性以使其显示在表单集合中并将 VALUE 更改为 VAL 还检查了表单的 ID(在我的情况下为 form1)
used the Firefox firebug to check whether the element was inserted.
使用 Firefox firebug 检查是否插入了元素。
Hidden elements do get posted back in the form collection, only read-only fields are discarded.
隐藏元素确实会在表单集合中回传,只有只读字段会被丢弃。
Michel
米歇尔
回答by Daff
In your case it should suffice to just add another hidden field to your form dynamically.
在您的情况下,只需将另一个隐藏字段动态添加到您的表单中就足够了。
var input = $("<input>").attr("type", "hidden").val("Bla");
$('#form').append($(input));
回答by Parag
You can even use this one. worked well for me
你甚至可以使用这个。对我来说效果很好
$("#registerform").attr("action", "register.php?btnsubmit=Save")
$('#registerform').submit();
this will submit btnsubmit =Save as GET value to register.php form.
这将提交 btnsubmit =Save as GET 值到 register.php 表单。
回答by Jonathan
You could write a jQuery function which allowed you to add hidden fields to a form:
您可以编写一个 jQuery 函数,它允许您向表单添加隐藏字段:
// This must be applied to a form (or an object inside a form).
jQuery.fn.addHidden = function (name, value) {
return this.each(function () {
var input = $("<input>").attr("type", "hidden").attr("name", name).val(value);
$(this).append($(input));
});
};
And then add the hidden field before you submit:
然后在提交之前添加隐藏字段:
var frm = $("#form").addHidden('SaveAndReturn', 'Save and Return')
.submit();
回答by PetersenDidIt
You don't need to bind the submit event on the click of the submit button just bind the submit event and it will capture the submit event no mater how it gets triggered.
您不需要在单击提交按钮时绑定提交事件,只需绑定提交事件,无论它如何触发,它都会捕获提交事件。
Think what you are wanting is to submit the sortable like you would via ajax. Try doing something like this:
想想你想要的是像通过ajax一样提交排序。尝试做这样的事情:
var form = $('#event').submit(function () {
$.each($('#attendance').sortable('toArray'),function(i, value){
$("<input>").attr({
'type':'hidden',
'name':'attendace['+i+']'
}).val(value).appendTo(form);
});
});
回答by Cacho Santa
Similar answer, but I just wanted to make it available for an easy/quick test.
类似的答案,但我只是想让它用于简单/快速的测试。
var input = $("<input>")
.attr("name", "mydata").val("go Rafa!");
$('#easy_test').append(input);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<form id="easy_test">
</form>