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

javascript - jQuery - get value of checked checkbox that has a certain class - Stack Overflow

programmeradmin0浏览0评论

I'd like to get the value of the checked checkbox that has the radioListMode class:

<label class="btn btn-secondary active">
    <input type="radio" class="radioListMode" value="cards" checked>Cards
</label>
<label class="btn btn-secondary">
    <input type="radio" class="radioListMode" value="pins">Pins
</label>

From this previous question, this is what I have tried and it is undefined:

console.log($('input[class="radioListMode"]:checked').value);

I do not wish to add a name because the value of this checkbox is only used for layout selection, not any data input.

I'd like to get the value of the checked checkbox that has the radioListMode class:

<label class="btn btn-secondary active">
    <input type="radio" class="radioListMode" value="cards" checked>Cards
</label>
<label class="btn btn-secondary">
    <input type="radio" class="radioListMode" value="pins">Pins
</label>

From this previous question, this is what I have tried and it is undefined:

console.log($('input[class="radioListMode"]:checked').value);

I do not wish to add a name because the value of this checkbox is only used for layout selection, not any data input.

Share Improve this question edited May 23, 2017 at 12:15 CommunityBot 11 silver badge asked Feb 23, 2016 at 2:25 davidtgqdavidtgq 4,01011 gold badges48 silver badges86 bronze badges 2
  • Have you tried $('input.class="radioListMode"]:checked').val() – progrAmmar Commented Feb 23, 2016 at 2:27
  • I don't understand why you wouldn't add an ID. The extra amount of data sent would not make much of a difference in bandwidth costs unless you were processing billions (with a b) of users a month. – Jon Commented Feb 23, 2016 at 2:27
Add a ment  | 

4 Answers 4

Reset to default 4

Since you seem to be using jQuery, stick with it:

$('input.radioListMode:checked').val();

Use .val() property

$('input[class="radioListMode"]:checked').val()

.value is not a jQuery function, instead use .val() like following :

console.log($('input[class="radioListMode"]:checked').val());
$(".yourClassName:checkbox:checked").each(function() {
     console.log($(this).attr("id"));
});
发布评论

评论列表(0)

  1. 暂无评论