Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在div中由javascript添加的dynamique表单_Javascript - Fatal编程技术网

在div中由javascript添加的dynamique表单

在div中由javascript添加的dynamique表单,javascript,Javascript,我在div中有一个由javascript通过id添加的dynamique表单,但问题是当我调用此函数来填充我的div时,第一个调用工作正常,但第二个它添加了已经存在的相同输入。然后在同一个div中,我有重复的输入 getAllPiece(); function getAllPiece(){ $.getJSON( "getAllPiece", {piece: $('select#typeevenement').val()}, function(data) {

我在div中有一个由javascript通过id添加的dynamique表单,但问题是当我调用此函数来填充我的div时,第一个调用工作正常,但第二个它添加了已经存在的相同输入。然后在同一个div中,我有重复的输入

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(
    “…
    )不可用,则可以使用
    $(“div.checkbox”)。删除()
    ,然后添加另一个div


  • 非常感谢你