我有以下HTML代码。我想得到每个additionalAddress(x)
的第一个地址,以构建一个漂亮的数组,以便我可以将它们插入到db中。这是jQuery append
逻辑的所有部分,因此地址可以是无限的。
<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>
答案 0 :(得分: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);
请参阅demo
答案 1 :(得分:0)
如果其他地址的数量是固定的,您可以使用
[
$("#Address").val(),
$("#additionalAddress1 > input").val(),
$("#additionalAddress2 > input").val(),
$("#additionalAddress3 > input").val()
]
假设您删除了input
范围内的additionalAddressX
元素的ID。
[edit]如果没有修复,您可以使用:
var addresses = [$("#Address").val()];
$("span[id^='additionalAddress'] > input[id^='Address']").each(function(i,e) {
addresses.push($(e).val());
});
假设input
范围内的additionalAddressX
元素包含AddressX
等ID。