0

I have numerous input boxes that I'm trying to store the names of into an array. I'm using this currently to get the names:

var getImplementedNames = function (selector){
    $(selector).each(function() {
        console.log($( this ).attr('name').replace('imp-', ''));
    });
}   

console.log(getImplementedNames('[id^=imp]'));

This works, but now I'd like to add all the reslts to an array. I've tried;

var array = [getImplementedNames('[id^=imp]')];

console.log(array);

Which returns an undefined array.

I'm not sure of how this is supposed to be properly handled.

4

2 回答 2

0

使用.map()

var getImplementedNames = function (selector) {
    return  $(selector).map(function () {
        return $(this).attr('name').replace('imp-', '');
    }).get();
}

用法

console.log(getImplementedNames('[id^=imp]'));

从 JavaScript 中的函数读取 返回值

于 2013-11-12T02:03:17.683 回答
0

您的函数当前没有返回任何内容。尝试:

var getImplementedNames = function (selector){
    return $(selector).map(function() {
        return $( this ).attr('name').replace('imp-', '');
    });
}   

console.log(getImplementedNames('[id^=imp]'));
于 2013-11-12T02:03:48.447 回答