JQuery迭代TextField_Counter

时间:2012-06-07 12:21:47

标签: javascript jquery dhtml

有人可以帮助您从下面收集表格数据。虽然我很难成功地从表单中提取数据,但是以下标记被查找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);

});

1 个答案:

答案 0 :(得分:0)

Doku:.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