2012-06-07 70 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

认为我已经完成了。 –