One of my form allows to add multiple elements using Jquery. Following HTML shows the demo content,
<form name="my-location-frm">
<div class="address">
<input type="text" name="house-name" value='house1'>
<input type="text" name="street-no" value='street1'>
</div>
<div class="address">
<input type="text" name="house-name" value='house2'>
<input type="text" name="street-no" value='street2'>
</div>
<div class="address">
<input type="text" name="house-name" value='house3'>
<input type="text" name="street-no" value='street3'>
</div>
<input type="submit">
</form>
Here class="address"
wrapper will repeats many times. How can retrieve each element (house-name,street-no) values using Jquery
Tried as follows,
$.each($(".address"), function(key,value) {
hn = $(value).children("input[name=house-name]").val();
console.log(n);
}
But fails :(
Expected Javascript output,
house1,street1
house2,street2
house3,street3
Use this variant instead:
$(".address").each(function() {
var house = $(this).children("input[name='house-name']").val();
var street = $(this).children("input[name='street-no']").val();
console.log(house + "," + street);
});
Or (if you need) you can gather all the input values in the array:
$(".address").each(function() {
var values = [];
$(this).children("input").each(function() {
values.push(this.value);
});
console.log(values.join(","));
});
$.each($(".address"), function(key,value) {
var hn = $(this).children('input[name="house-name"]').val(),
sn = $(this).children('input[name="street-no"]').val();
console.log(hn.concat(', ' + sn));
});
OR
$.each($(".address"), function(key,value) {
var hn = $('input[name="house-name"]', this).val(),
sn = $('input[name="street-no"]', this).val();
console.log(hn.concat(', ' + sn));
});
OR
$.each($('.address'), function() {
var output = $('input[name="house-name"]', this).val().concat(', ' + $('input[name="street-no"]', this).val());
console.log(output);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.