我有很多输入框,我正在尝试将这些名称存储到数组中。我目前正在使用它来获取名称:
var getImplementedNames = function (selector){
$(selector).each(function() {
console.log($( this ).attr('name').replace('imp-', ''));
});
}
console.log(getImplementedNames('[id^=imp]'));
这有效,但现在我想将所有reslts添加到数组中。我试过了;
var array = [getImplementedNames('[id^=imp]')];
console.log(array);
返回未定义的数组。
我不确定这应该如何正确处理。
答案 0 :(得分:1)
使用.map()
var getImplementedNames = function (selector) {
return $(selector).map(function () {
return $(this).attr('name').replace('imp-', '');
}).get();
}
使用
console.log(getImplementedNames('[id^=imp]'));
答案 1 :(得分:1)
您的功能目前尚未返回任何内容。尝试:
var getImplementedNames = function (selector){
return $(selector).map(function() {
return $( this ).attr('name').replace('imp-', '');
});
}
console.log(getImplementedNames('[id^=imp]'));