我是JavaScript和jQuery的新手,所以请耐心等待。
首先,这是我的代码,以便您可以测试它以查看错误:http://jsfiddle.net/Lzw4e/1/
我希望每次用户从左侧<select>
元素中选择时创建一个新的隐藏字段,并在用户点击右侧<select>
元素时删除/销毁隐藏字段。
我使用了jQuery命令$("<input type='hidden' value=selectedAddFootballPlayerId>");
但是当我检查Firebug时,我看不到任何隐藏字段被创建。为了删除隐藏的字段,我真的不知道。
请帮帮我。提前谢谢。
答案 0 :(得分:6)
为此,您可以使用.append()
。
$("body").append("<input type='hidden' value=selectedAddFootballPlayerId>");
要删除,请使用.remove()
。
$("input[type='hidden']").remove();
使用我的示例时要小心,因为它会删除所有隐藏的表单元素。如果你想要更多的预决定,你可以为隐藏的输入分配一个id
值,然后在第二个例子中将其作为你的选择器。
答案 1 :(得分:5)
创建 -
var $ip = $('<input>').attr({
type: 'hidden',
id: 'yourid',
name: 'yourname',
value: 'yourvalue'
})
$(ip).appendTo('body');
然后删除 -
$ip.remove();
答案 2 :(得分:2)
您必须附加字段:
$("<input type='hidden' value=selectedAddFootballPlayerId>").appendTo('#someSelector');
答案 3 :(得分:2)
我认为在定义选择器或要显示新项目的位置时会感到困惑。试试这个(我使用文字输入):
答案 4 :(得分:1)
工作版
更改
1
$("<input type='hidden' value=selectedAddFootballPlayerId>");
to
$('body').append("<input type='hidden' value=\""+selectedAddFootballPlayerId+"\">");
2
$('#listboxFootballPlayers').append(option);
to
$('#listboxFootballPlayers').append(option);
$('input[type="hidden"][value="'+selectedRemoveFootballPlayerId+'"]').remove();
答案 5 :(得分:0)
您可以尝试以下方法:
<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);