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 に答える