0

I have the below HTML code. I would like to get the first address then for each additionalAddress(x) to build a nice array so that I can insert them into db. This is all part of an jQuery append logic so the addresses can be unlimited.

<div class="controls" id="Locations">

<input class="span7" type="text" placeholder="Full Address" id="Address" style="margin: 0px 5px 5px 0;"><br>
<input class="span3" type="text" placeholder="(555) 555-5555" id="Phone" name="Phone" onkeyup="drawPhone(this);" style="margin: 0px 5px 5px 0;">
<input class="span4" type="text" placeholder="name@mybusiness.com" id="Email" onkeyup="drawEmail(this);"> <i class="icon-plus-sign" id="addAddress"></i>

<span id="additionalAddress1"><br><br>
    <input class="span7" type="text" placeholder="Full Address" id="Address1"><br><input class="span3" type="text" placeholder="(555) 555-5555" id="Phone1" name="Phone"><input class="span4" type="text" placeholder="name@mybusiness.com" id="Email1" > <i class="icon-minus-sign" id="removeAddress"></i></span>

<span id="additionalAddress2"><br><br><input class="span7" type="text" placeholder="Full Address" id="Address2"><br><input class="span3" type="text" placeholder="(555) 555-5555" id="Phone2" name="Phone"><input class="span4" type="text" placeholder="name@mybusiness.com" id="Email2" style="margin: -4px 0px 0 11px;"> <i class="icon-minus-sign" id="removeAddress"></i></span>

<span id="additionalAddress3"><br><br><input class="span7" type="text" placeholder="Full Address" id="Address3"><br><input class="span3" type="text" placeholder="(555) 555-5555" id="Phone3" name="Phone"><input class="span4" type="text" placeholder="name@mybusiness.com" id="Email3"> <i class="icon-minus-sign" id="removeAddress"></i></span>

</div>
4

2 回答 2

0
var address = new Array();
$("#Locations").find('span[id^="additionalAddress"]').each(function() {
    var temp = " " + $(this).find("input[id^='Address']").val();
    address.push(temp);
});
alert(address);

查看演示

于 2013-08-22T06:34:25.563 回答
0

如果附加地址的数量是固定的,您可以使用

[
    $("#Address").val(),
    $("#additionalAddress1 > input").val(),
    $("#additionalAddress2 > input").val(),
    $("#additionalAddress3 > input").val()
]

假设您删除了跨度input内元素的 ID。additionalAddressX

[编辑] 如果不固定,您可以使用:

var addresses = [$("#Address").val()];
$("span[id^='additionalAddress'] > input[id^='Address']").each(function(i,e) {
    addresses.push($(e).val());
});

假设跨度input内的元素additionalAddressX具有像AddressX.

于 2013-08-22T06:36:21.323 回答