在div中由javascript添加的dynamique表单
我在div中有一个由javascript通过id添加的dynamique表单,但问题是当我调用此函数来填充我的div时,第一个调用工作正常,但第二个它添加了已经存在的相同输入。然后在同一个div中,我有重复的输入在div中由javascript添加的dynamique表单,javascript,Javascript,我在div中有一个由javascript通过id添加的dynamique表单,但问题是当我调用此函数来填充我的div时,第一个调用工作正常,但第二个它添加了已经存在的相同输入。然后在同一个div中,我有重复的输入 getAllPiece(); function getAllPiece(){ $.getJSON( "getAllPiece", {piece: $('select#typeevenement').val()}, function(data) {
getAllPiece();
function getAllPiece(){
$.getJSON(
"getAllPiece",
{piece: $('select#typeevenement').val()},
function(data) {
var html = '';
for(var i=0; i < data.length; i++){
html += '<div class="checkbox"><label><input type="checkbox" class="piece" name="piece" value="'+data[i].codePiece+'" id="copiercin"/>' + data[i].libellePiece + '</label></div>';
console.log(html);
}
$('#divpiece').append(html);
}
);
}
getAllPiece();
函数getAllPiece(){
$.getJSON(
“getAllPiece”,
{piece:$('select#typeevennement').val(),
功能(数据){
var html='';
对于(变量i=0;i
如果在“#divpiece”
元素中没有其他内容,您可以使用html()
方法代替append()
,因为它还删除了您要添加html的元素的内容。所以它看起来像:
...
function(data) {
var html = '';
for(var i=0; i < data.length; i++){
html += '<div class="checkbox"><label><input type="checkbox" class="piece" name="piece" value="'+data[i].codePiece+'" id="copiercin"/>' + data[i].libellePiece + '</label></div>';
console.log(html);
}
$("#divpiece").html(html); // HERE IS THE CHANGE
}
);
。。。
功能(数据){
var html='';
对于(变量i=0;i
如果需要追加(如果添加的元素不止一个),则有两种可能的解决方案:
“#divpiece”
是否已在其内部包含该元素,如if($(“.piece”).val()==data[i].codepece){//stop script}
“…
)不可用,则可以使用$(“div.checkbox”)。删除()
,然后添加另一个div非常感谢你