Javascript 如何使用Jquery从动态创建的文本框中检索值
我的一个表单允许使用Jquery添加多个元素。下面的HTML显示了演示内容Javascript 如何使用Jquery从动态创建的文本框中检索值,javascript,jquery,css,Javascript,Jquery,Css,我的一个表单允许使用Jquery添加多个元素。下面的HTML显示了演示内容 <form name="my-location-frm"> <div class="address"> <input type="text" name="house-name" value='house1'> <input type="text" name="street-no" value='street1'> </d
<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>
但是失败了:(
预期的Javascript输出
house1,street1
house2,street2
house3,street3
或
或
或
或
改用此变体:
$(".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);
});
或者(如果需要),可以收集数组中的所有输入值:
$(".address").each(function() {
var values = [];
$(this).children("input").each(function() {
values.push(this.value);
});
console.log(values.join(","));
});
演示:改用此变体:
$(".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);
});
或者(如果需要),可以收集数组中的所有输入值:
$(".address").each(function() {
var values = [];
$(this).children("input").each(function() {
values.push(this.value);
});
console.log(values.join(","));
});
演示:
$(".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);
});
$(".address").each(function() {
var values = [];
$(this).children("input").each(function() {
values.push(this.value);
});
console.log(values.join(","));
});