2012-06-07 93 views
0

有人可以幫忙收集下面的表單數據。儘管我正在努力從表單中成功提取數據,但以下標記是爲了尋找x個客戶。JQuery遍歷TextField_Counter

非常感謝, 詹姆斯

<h5 id="Customer1"> 
    <span>Customer 1</span> 
</h5> 

<div class="CustomerWrapper"> 
<input type="hidden" value="0" id="CustomerType0" name="CustomerType0"> 

<span class="TitleSelect"> 
    <label for="CustomerTitle0">Title</label> 
    <select id="CustomerTitle0" name="CustomerTitle0"> 
    <option value="-1"></option> 
    <option value="1">Mr</option> 
    <option value="2">Mrs</option> 
    <option value="3">Ms</option> 
    <option value="4">Miss</option> 
    </select>         
</span> 

<span class="FirstInput"> 
    <label for="FirstName0">First Name</label> 
    <input type="text" value="" name="FirstName0" id="FirstName0"> 
</span> 

<span class="LastInput"> 
    <label for="LastName0">Surname(s)</label> 
    <input type="text" value="" name="LastName0" id="LastName0"> 
</span> 

我嘗試faily接近我需要的只是第一和lastnames。

$('.PaxDetails .CustomerWrapper').each(function (index) { 

    var counter = ++index; 
    var firstName = "#FirstName" + counter; 
    var lastName = "#LastName" + counter; 

    var customerName = $(firstName).val() + ' ' + $(lastName).val(); 

    alert(customerName); 

}); 

回答

0

數獨:.serialize()

$("<selector to get the form>").serialize(); 

編輯
如果您只需要在第一個和最後的名字,那麼你將不得不做手工......

var customer = []; 

$("div.CustomerWrapper").each(function(i, el) { 
    customer.push({ 
     "FirstName": $("#FirstName" + i, el).val(), 
     "LastName": $("#LastName" + i, el).val() 
    }); 
}); 

console.log(customer); 

jsfiddle

+0

這似乎幾乎工作,因爲我只需要建立第一個和最後一個名字的集合 –

+0

我需要將每個客戶的名字和姓氏添加到數組中?不知道這將如何工作 –

+0

認爲我已經完成了。 –