2013-01-25 27 views
0

有一個更新的textarea代碼:創建jQuery的陣列

var input_baseprice = $('.active .input_baseprice').html(); 

$('[name=baseprice]').html(input_baseprice); 

然而,當有很多.input_baseprice元素,textarea的只有從第一個獲取內容。

我如何可以自動的創建input_baseprice*獲得:

$('[name=baseprice]').html(input_baseprice + '<br>' + input_baseprice2 + '<br>' + input_baseprice3 ...); 

+0

所以很多偉大的變種!非常感謝所有人。 –

回答

3

即在API文檔指出http://api.jquery.com/html/

爲了使的以下內容進行檢索

試試這個

var html = ''; 

$('.active .input_baseprice').each(function() { 
    html += $(this).html(); 
}); 

$('[name=baseprice]').html(html); 

後重新閱讀您的問題,您可能需要將我們的.html()替換爲.val(),具體取決於您處理的元素的類型

1

使用創建陣列中的每個循環thrrough textarea的內容... .val()應該工作 嘗試在此

var str=""; 

$('.active .input_baseprice').each(function(){ 
     str += $(this).val(); 
}); 

$('[name=baseprice]').val(str); 

OR

陣列..

var temparray= []; 

$('.input_baseprice').each(function(){ 
    temparray.push($(this).html()); 
} 

$('[name=baseprice]').html(temparray.join('<br>')); 
1

使用每個()函數來通過每個元件環和數據追加到一個數組,然後將被組合成一個字符串:

var data = []; 

$('.input_baseprice').each(function(){ 
    data.push($(this).html()); 
} 

$('[name=baseprice]').html(data.join('<br>')); 
2

使用.map方法,然後加入他們。

$('[name=baseprice]').html($('.active .input_baseprice').map(function() { 
    return $(this).html(); 
}).get().join('<br>'));