Javascript 如何使用 jQuery 创建和删除 HTML 隐藏字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8195888/
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
How to create and remove an HTML hidden field using jQuery?
提问by NinjaBoy
First of all here is my code so that you can test it to see what's wrong: JSFiddle
首先这是我的代码,以便您可以测试它以查看问题所在:JSFiddle
I want to create a new hidden field every time the user selects from the left <select>element and remove / destroy the hidden field when the user clicks the right <select>element.
我想每次用户从左侧<select>元素中选择时创建一个新的隐藏字段,并在用户单击右侧<select>元素时删除/销毁隐藏字段。
I used the jQuery command $("<input type='hidden' value=selectedAddFootballPlayerId>");but when I checked of Firebug I can't see any hidden field being created. For removal of the hidden field I really don't know.
我使用了 jQuery 命令,$("<input type='hidden' value=selectedAddFootballPlayerId>");但是当我检查 Firebug 时,我看不到任何正在创建的隐藏字段。对于去除隐藏字段,我真的不知道。
采纳答案by Galled
I think you are confused when defining the selector or where you want to display your new item. Try with this (I use text inputs):
我认为您在定义选择器或要在何处显示新项目时感到困惑。试试这个(我使用文本输入):
回答by esqew
For this you can use .append().
为此,您可以使用.append().
$("body").append("<input type='hidden' value=selectedAddFootballPlayerId>");
For removal, use .remove().
要移除,请使用 .remove().
$("input[type='hidden']").remove();
Be careful when using my example, as it'll remove all form elements that are hidden. If you want more prescision, you can assign an idvalue to the hidden input and then call that as your selector in the second example.
使用我的示例时要小心,因为它会删除所有隐藏的表单元素。如果您想要更精确,您可以id为隐藏输入分配一个值,然后在第二个示例中将其称为选择器。
回答by ipr101
To create -
创造 -
var $ip = $('<input>').attr({
type: 'hidden',
id: 'yourid',
name: 'yourname',
value: 'yourvalue'
})
$(ip).appendTo('body');
Then to remove -
然后删除 -
$ip.remove();
回答by Sgoettschkes
You have to append the field:
您必须附加该字段:
$("<input type='hidden' value=selectedAddFootballPlayerId>").appendTo('#someSelector');
回答by Utku Y?ld?r?m
Working version
工作版本
Changes
变化
1
1
$("<input type='hidden' value=selectedAddFootballPlayerId>");
to
$('body').append("<input type='hidden' value=\""+selectedAddFootballPlayerId+"\">");
2
2
$('#listboxFootballPlayers').append(option);
to
$('#listboxFootballPlayers').append(option);
$('input[type="hidden"][value="'+selectedRemoveFootballPlayerId+'"]').remove();
回答by Navdeep Paliwal
You can try this:
你可以试试这个:
<input type="hidden" name="image" id="input-image{{ image_row }}" />
inputt= "<input type="hidden" name="product_image' value="abcd">"
$("#input-image"+row).remove().append(inputt);

