最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting value of a "object HTMLInputElement" and function error - Stack Overflow

programmeradmin3浏览0评论

I have checkboxes on a page and I get the checked ones then I loop through them;

var checkeds = $('#accTypes input:checked');
    var values = "";

    for (var i = 0; i < checkeds.length; i++) {
        console.log(checkeds[i]);
        var cval = checkeds[i].val();
        values = values + "," + cval;
    }

I recognized that the row below causes error.

checkeds[i].val()

When I print the checkeds[i] variable in chrome console, I see;

<input type=​"checkbox" name=​"ac-check" id=​"checkboxP12" value=​"12">​

I wanted to get the value of checkeds[i] variable. How can I do this?

I have checkboxes on a page and I get the checked ones then I loop through them;

var checkeds = $('#accTypes input:checked');
    var values = "";

    for (var i = 0; i < checkeds.length; i++) {
        console.log(checkeds[i]);
        var cval = checkeds[i].val();
        values = values + "," + cval;
    }

I recognized that the row below causes error.

checkeds[i].val()

When I print the checkeds[i] variable in chrome console, I see;

<input type=​"checkbox" name=​"ac-check" id=​"checkboxP12" value=​"12">​

I wanted to get the value of checkeds[i] variable. How can I do this?

Share Improve this question asked Sep 7, 2014 at 11:42 Serhat KorogluSerhat Koroglu 1,2754 gold badges19 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

A jQuery collection is an array-like object containing native DOM nodes.

When you access it as checkeds[1] you get the native DOM node, not the jQuery version, so it doesn't have a val() method.

Either use the native value

var cval = checkeds[i].value;

or use eq() to get the jQuery object

var cval = checkeds.eq(i).val();

As a sidenote, you could do the same thing with a map

var values = $('#accTypes input:checked').map(function() {
    return this.value;
}).get().join(',');
发布评论

评论列表(0)

  1. 暂无评论