Jquery 获取 Div 的嵌套输入,放置在对象中
Jquery getting nested inputs of Div, placing in Object
我对这个嵌套循环束手无策。
我正在尝试创建一个对象,其中嵌套对象按包含的 div id 分组。
到目前为止,这就是我所拥有的:
$('#survey-submit').on('click', function (e) {
var formData = {};
var item = {};
$("div[id^='question-']").each(function () {
var id = '#' + $(this).attr('id');
var $this = $(this);
formData[id] = $this.children('input, select,checkbox').each(function () {
item[this.name] = this.value;
});
//console.debug(formData);
});
console.debug(formData);
return false;
});
控制台日志的结果是 divs 的所有输入元素,这些元素都有开头的问题,我得到了预期数量的新对象,它们都是动态命名的,但它们都包含相同的东西.
每个对象的内容并不特定于我试图从中生成它的 this 对象,我们将不胜感激!
我在这里找到了问题的解决方案:jquery how to get form element types, names and values
我最后这样做了:
$("div[id^='question-']").each(function () {
var id = '#' + $(this).attr('id');
var $this = $(this);
var inputs= $(id+' :input').map(function(index, elm) {
return {name: elm.name, value: $(elm).val()};
}).get();
formData[id]=inputs;
});
我对这个嵌套循环束手无策。 我正在尝试创建一个对象,其中嵌套对象按包含的 div id 分组。 到目前为止,这就是我所拥有的:
$('#survey-submit').on('click', function (e) {
var formData = {};
var item = {};
$("div[id^='question-']").each(function () {
var id = '#' + $(this).attr('id');
var $this = $(this);
formData[id] = $this.children('input, select,checkbox').each(function () {
item[this.name] = this.value;
});
//console.debug(formData);
});
console.debug(formData);
return false;
});
控制台日志的结果是 divs 的所有输入元素,这些元素都有开头的问题,我得到了预期数量的新对象,它们都是动态命名的,但它们都包含相同的东西.
每个对象的内容并不特定于我试图从中生成它的 this 对象,我们将不胜感激!
我在这里找到了问题的解决方案:jquery how to get form element types, names and values
我最后这样做了:
$("div[id^='question-']").each(function () {
var id = '#' + $(this).attr('id');
var $this = $(this);
var inputs= $(id+' :input').map(function(index, elm) {
return {name: elm.name, value: $(elm).val()};
}).get();
formData[id]=inputs;
});