2013-02-13 105 views
0

我有以下div標籤與子標籤。JQuery追加新的div到其父母

<div class="span6"> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Item Name</label> 
     <div class="controls"> 
      <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> 
     </div> 
    </div> 
</div> 

我想根據陣列追加孩子<div class="control-group"></div><div class="span6"></div>。所以我寫了JavaScript如下。

$.each(categories, function (k, elem) { 
    var options = ''; 
    var options = ' 
    <div class="control-group"> 
     <label for="StockCategory">Category</label> 
     <div class="controls"> 
      <select class="input-xlarge get-category"> 
    '; 
    options +='<option value="">Select Category</option>'; 
    $.each(data, function(key, value) { 
     options += '<option value="' + key + '">' + value + '</option>'; 
    }); 
    options +=' 
      </select> 
     </div> 
    </div>'; 
    $("div.span6").append(options); 
} 

所以我想根據數組追加這些子div,但沒有發生。所以我需要以下結果。

<div class="span6"> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Item Name</label> 
     <div class="controls"> 
      <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Item Name</label> 
     <div class="controls"> 
      <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Item Name</label> 
     <div class="controls"> 
      <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Item Name</label> 
     <div class="controls"> 
      <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> 
     </div> 
    </div> 
    <div class="control-group"> 
     <label class="control-label" for="inputEmail">Item Name</label> 
     <div class="controls"> 
      <input type="text" id="inputEmail" class="input-xlarge" placeholder="Enter item name here..."> 
     </div> 
    </div> 
</div> 

請幫我解決。這項工作更受讚賞。

+0

我不明白...'所以我需要下面的結果.' ...我沒有看到任何與'StockCategory'等等......你是說你只需要克隆第一個div ...看到你的結果看起來像這就是你想要的...... – bipen 2013-02-13 10:46:29

+0

和你的'數據'第二個每個循環? – bipen 2013-02-13 10:48:12

回答

1

下面jQuery將做要緊。

<script type="text/javascript"> 
$(document).ready(function(){ 

    var data = { val1 : 'text1', val2 : 'text2', val3 : 'text3' }; 
    var options = ''; 
    $.each(data, function(key, value) { 

/* options = '<div class="control-group"><label for="StockCategory">Category</label><div class="controls"><select class="input-xlarge get-category">'; 
    options += '<option value="">Select Category</option>'; 
    options += '<option value="' + key + '">' + value + '</option>'; 
    options +='</select></div></div>'; 
*/ 


    options = '<div class="control-group"><label class="control-label" for="' + key + '">' + key + '</label><div class="controls">'; 
    options +='<input type="text" id="' + key + '" class="input-xlarge" placeholder="' + value + '">'; 
    options +='</div></div>'; 



    $("div.span6").append(options); 

    }); 
}); 
</script> 
1

我首先要看的是span6缺少一個關閉</div> - 這是否解決了這個問題?

1

儘量把追加的代碼$。每個

var options = '';   
$.each(categories, function (k, elem) { 
var options = ' 
<div class="control-group"> 
    <label for="StockCategory">Category</label> 
    <div class="controls"> 
     <select class="input-xlarge get-category"> 
      '; 
      options +='<option value="">Select Category</option>'; 
      $.each(data, function(key, value) { 
      options += '<option value="' + key + '">' + value + '</option>'; 
      }); 
      options +=' 
     </select> 
    </div> 
</div>'; 

} 
$("div.span6").append(options); 
1

外嘗試改變你的腳本

var options = ''; 

$.each(categories, function (k, elem) { 
    options += ' 
    <div class="control-group"> 
     <label for="StockCategory">Category</label> 
     <div class="controls"> 
      <select class="input-xlarge get-category"> 
       <option value="">Select Category</option> 
    '; 
    $.each(data, function(key, value) { 
     options += '<option value="' + key + '">' + value + '</option>'; 
    }); 
    options +=' 
      </select> 
     </div> 
    </div>'; 
} 

$("div.span6").append(options);