[英]Creating Object Key with jquery.each() loop
I'm creating a namespace in javascript to loop through a form and create an object. 我正在javascript中创建一个名称空间来遍历表单并创建一个对象。 The goal of the function when called is to loop through all of a certain form type and construct an object that has a key which is the html input's name and the value as its current value.
调用函数的目的是遍历所有特定的表单类型,并构造一个具有键的对象,该键是html输入的名称,值是其当前值。 However, it keeps returning undefined.
但是,它不断返回undefined。
Any help would be greatly appreciated: 任何帮助将不胜感激:
get_form_data.radio = function(container) { //will return the value
var data = {}; //function data object to return
container.find('input[type="radio"]:checked').each(function() {
var current_object = {}; //loop temporary object
var current = $(this); //current element
var current_key = current.attr('name'); //property category
var current_value = current.attr('value'); //value to update the database with
current_object[current_key] = current_value; //temporary object
console.log(current_object.length); //RETURNS UNDEFINED
$.extend(data, current_object);
});
console.log(data.length); //returns undefined
return data;
}
You want to get the var current_object = {};
你想得到
var current_object = {};
declaration out of the .each()
call. 声明退出
.each()
调用。 Each iteration of the .each()
function redeclares it, and effectively erases it. .each()
函数的每次迭代.each()
重新声明它,并有效地删除它。 And forget about $.extend()
as well. 并忘记
$.extend()
。
var data = {};
container.find('input[type="radio"]:checked').each(function() {
data[this.name] = this.value;
});
return data;
Untested though from a quick skim of your current code. 通过快速浏览当前代码未经测试。
You need specify key and index value, something like thtat: 您需要指定键和索引值,如thtat:
array.each(function(index, value) {
alert(index + ': ' + value);
});
In your case: 在你的情况下:
get_form_data.radio = function(container) { //will return the value
var data = {}; //function data object to return
container.find('input[type="radio"]:checked').each(function(index,value) {
var current_object = {}; //loop temporary object
var current = value; //current element
var current_key = current.attr('name'); //property category
var current_value = current.attr('value'); //value to update the database with
current_object[current_key] = current_value; //temporary object
console.log(current_object.length); //RETURNS UNDEFINED
$.extend(data, current_object);
});
console.log(data.length); //returns undefined
return data;
}
Problem solved by taking a look at the global scope. 通过查看全局范围解决了问题。 It seems that the code above worked but my global namespace was confusing
current = $(this)
inside the each loop and this.data which is the global object for form data. 看起来上面的代码工作但是我的全局命名空间在每个循环中混淆
current = $(this)
和this.data,它是表单数据的全局对象。
heres my form_submit namespace: 继承我的form_submit命名空间:
this.data = {};
this.property_status = function() {
var property_status = {};
this.container.children('form').each(function() {
var current = $(this);
property_status[current.attr('data-property_id')] = get_form_data.radio(current);
});
$.extend(this.data, property_status);
};
and the get_form_data namespace: 和get_form_data命名空间:
get_form_data.radio = function(container) {//will return the value
var form_data = {};//function data object to return
container.find('input[type="radio"]:checked').each(function() {
form_data[this.name] = this.value;
});
return form_data;
}
Any suggestions on optimizing this? 关于优化这个的任何建议?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.