[英]Getting undefined value from input field
So I have a page where people will be able to randomly generate some input fields, so I need to get the value of every single input and send it to my php page. 因此,我有一个页面,人们可以在其中随机生成一些输入字段,因此我需要获取每个输入的值并将其发送到我的php页面。
The tricky part is that I don't know how many inputs will there be in the end, and therefor I need some kind of a loop to pick all the values up. 棘手的是,我不知道最后会有多少输入,因此我需要某种循环来拾取所有值。 Here is what I have right now, everything seems to be ok and I do manage to get all the input fields.
这就是我现在所拥有的,一切似乎都还可以,我确实设法获得了所有输入字段。 But for some reason values of every input field seems to be undefined, does anyone know why?
但是由于某些原因,每个输入字段的值似乎都未定义,有人知道为什么吗?
$(document).on('click', '#izmena', function(){
$(function() {
var collectionA = $('.crtezInput');
var datas = [];
$.each(collectionA, function(idx, obj) {
datas.push({ 'input': $(obj).find('input').val() });
});
$.each(datas, function(idx, obj) {
crtezNaziv += obj.input +",";
});
console.log(crtezNaziv);
});
<div id="izmena_crteza">
<div class="headlinea">Crtezi</div>
<div class="crtez-form">
<input type="text" style="width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Naziv" value="Glavni izgled">
<i title="Obrisi Arhitektu" style="margin-top: -18px;" class="fa fa-times fa-2x deleteArh"></i>
<input type="text" style="margin-top:-20px; width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Razmera" value="1:100">
<input type="text" style="width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Tehnika" value="Tus">
</div>
<div class="crtez-form">
<input type="text" style="width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Naziv" value="Izgled sa strane">
<i title="Obrisi Arhitektu" style="margin-top: -18px;" class="fa fa-times fa-2x deleteArh"></i>
<input type="text" style="margin-top:-20px; width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Razmera" value="1:100">
<input type="text" style="width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Tehnika" value="Tus">
</div>
<div class="crtez-form">
<input type="text" style="width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Naziv" value="Presek a-b">
<i title="Obrisi Arhitektu" style="margin-top: -18px;" class="fa fa-times fa-2x deleteArh"></i>
<input type="text" style="margin-top:-20px; width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Razmera" value="1:100">
<input type="text" style="width:80%; margin-bottom:10px;" class="form-control upis-style crtezInput" placeholder="Tehnika" value="Tus">
</div>
</div>
You're getting undefined
because you're looking for input
elements within your input
elements: 你得到
undefined
,因为你正在寻找input
您内的元素input
元素:
var collectionA = $('.crtezInput');
var datas = [];
$.each(collectionA, function(idx, obj) {
datas.push({'input': $(obj).find('input').val()});
// Here -------------^^^^^^^^^^^^^^^^^^^^^^^^^^
});
Since you don't (and indeed can't ) have input
elements within other input
elements, find
doesn't find anything, and val
returns undefined
. 既然你不(实际上不可能 ),具有
input
等中的元素input
元素, find
并没有发现任何和val
返回undefined
。 (This is the only case where val
returns undefined
, btw: When the set you call it on has no elements in it. Otherwise it always returns a string, possibly a blank one.) (这是
val
返回undefined
的唯一情况,btw:当您调用它的集合中没有元素时,否则它总是返回一个字符串,可能为空。)
Here's the minimal change: 这是最小的更改:
var collectionA = $('.crtezInput');
var datas = [];
$.each(collectionA, function(idx, obj) {
datas.push({'input': $(obj).val()});
});
...but I'd probably make that a bit more direct: ...但是我可能会更直接一点:
var collectionA = $('.crtezInput');
var datas = collectionA.map(function() {
return {input: this.value};
}).get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.