使用jQuery动态创建隐藏输入表单字段的最简单方法是什么?
当前回答
如果你想添加更多的属性,就像这样做:
$('<input>').attr('type','hidden').attr('name','foo[]').attr('value','bar').appendTo('form');
Or
$('<input>').attr({
type: 'hidden',
id: 'foo',
name: 'foo[]',
value: 'bar'
}).appendTo('form');
其他回答
$('#myformelement').append('<input type="hidden" name="myfieldname" value="myvalue" />');
和David的一样,但是没有attr()
$('<input>', {
type: 'hidden',
id: 'foo',
name: 'foo',
value: 'bar'
}).appendTo('form');
$('<input>').attr('type','hidden').appendTo('form');
回答你的第二个问题:
$('<input>').attr({
type: 'hidden',
id: 'foo',
name: 'bar'
}).appendTo('form');
如果你想添加更多的属性,就像这样做:
$('<input>').attr('type','hidden').attr('name','foo[]').attr('value','bar').appendTo('form');
Or
$('<input>').attr({
type: 'hidden',
id: 'foo',
name: 'foo[]',
value: 'bar'
}).appendTo('form');
function addHidden(theForm, key, value) {
// Create a hidden input element, and append it to the form:
var input = document.createElement('input');
input.type = 'hidden';
input.name = key; //name-as-seen-at-the-server
input.value = value;
theForm.appendChild(input);
}
// Form reference:
var theForm = document.forms['detParameterForm'];
// Add data:
addHidden(theForm, 'key-one', 'value');