我试图创建一个消息列表,用户可以在网站上的textarea中键入并使用他自动创建的每个消息的按钮删除它们中的任何一个。 我的想法是,我创建了一个div,首先将用户的消息放入,然后预先添加与div相同ID的按钮;这样我调用按钮的ID并删除具有相同ID的所有元素。使用jQuery/JS创建的按钮,请不要点击
<script>
var theButtonId = 1;
$(document).ready(function() {
$('#commentButton1').click(function() {
var toAdd = $("textarea#commentTextarea1").val(); //Content to add
var theId = "dieButtons"+theButtonId.toString(); //creating unique ID
// Adding first Div
$('#targetField').append(
$('<div />', {
id: theId,
}));
// Adding content
$('#'+theId).append(
$('<div />', {
text: toAdd
}));
//(-----1-----)Adding Button
$('#'+theId).prepend(
$('<button />', {
type: 'button',
id: theId,
class: 'commentButton2',
text: "-"
}));
theButtonId++;
});
});
//Button Command
$(document).on("ready", function(){
//(-----2-----)
$('.commentButton2').on("click", function(){
var thisId = $(this).attr("id");
$("#"+thisId).remove();
});
});
</script>
它完美地列出了#textfield 元件我直接在其中删除某些的div,以及本身使用上面的代码我的HTML代码添加一个按钮。这就是为什么我知道问题是(----- 1 -----)中创建的按钮不对(----- 2 -----)
中的命令作出反应我已经尝试创建一个输入类型按钮,并把一个.click函数,而不是.on(“点击”,[...])。上动态创建的元素
@Popnoodles你链接的问题也被标记为重复:) –
'ids'意味着是唯一 – dreamweiver
@dreamweiver 我不好,我刚开始编码两个星期前,并监督该^^” 感谢您指出它出:) :) – sankai