0

有人可以帮忙从下面收集表格数据吗?尽管我正在努力从表单中成功提取数据,但以下标记是针对 x 数量的客户进行的。

非常感谢,詹姆斯

<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>

我的尝试失败了,因为我只需要名字和姓氏。

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

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

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

    alert(customerName);

});
4

1 回答 1

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

于 2012-06-07T12:26:10.273 回答