2011-04-05 52 views
0

我怎樣才能使數組中的這個簡單?:jQuery和串聯多個域值

var address = jQuery.trim($("#Address1").val()) + " " + jQuery.trim($("#City").val()) + " " + jQuery.trim($("#State").val()) + " " + jQuery.trim($("#Zip").val()); 

回答

3

試試這個:

var address = ""; 
$("#Address1, #City, #State, #Zip").each(function(){ 
    address += $.trim($(this).val()) + " "; 
}); 

如果你正在尋找表格文本輸入,那麼你可以使它更簡單,如下所示:

var address = ""; 
$(":text").each(function(){ 
    address += $.trim($(this).val()) + " "; 
}); 
+0

感謝您的快速回復 – 2011-04-05 17:24:52

1

堆棧項目和循環:

var arr = ['Address1', 'City', 'State', 'Zip']; 
for(var i=0;i<arr.length;i++){ 
    arr[i] = $('#'+arr[i]).val(); 
} 
var address = arr.join(' '); 
+0

感謝您的快速回復 – 2011-04-05 17:24:25