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

javascript - Get value attribute of the clicked checkbox via JQuery - Stack Overflow

programmeradmin0浏览0评论

I have got this checkbox which has value 1.

<input type="checkbox" name="option_1" id="checkbox_1" value="1">

Also I use this method to make checked/unchecked it.

$('input[id^="checkbox_"]').not('#checkbox_all').click(function () {
            $('#checkbox_all').prop('checked', false);

    // Get 'VALUE' of the checkbox here   

});

What I need is somehow get 'VALUE' of the clicked checkbox. So In that case it should be 1.

Any clue how do it could be done?

Thank you!

I have got this checkbox which has value 1.

<input type="checkbox" name="option_1" id="checkbox_1" value="1">

Also I use this method to make checked/unchecked it.

$('input[id^="checkbox_"]').not('#checkbox_all').click(function () {
            $('#checkbox_all').prop('checked', false);

    // Get 'VALUE' of the checkbox here   

});

What I need is somehow get 'VALUE' of the clicked checkbox. So In that case it should be 1.

Any clue how do it could be done?

Thank you!

Share Improve this question asked Aug 14, 2012 at 12:48 NoWarNoWar 37.6k82 gold badges337 silver badges514 bronze badges 1
  • possible duplicate of Get checkbox value in jQuery – Felix Kling Commented Aug 14, 2012 at 12:59
Add a comment  | 

4 Answers 4

Reset to default 19

In your click method use this to get the value

$(this).attr("value");

$(this) is referencing to the object that has been clicked.

EDIT: you could also use $(this).val(); but sometimes I had problems with elder versions of IE so I did recommend $(this).attr("value") in the first place.

​<html>

​<head>

​</head>

<body>
    <input type="checkbox" name="option_1" id="checkbox_1" value="1">
</body>

​</html>​​​​​​​

$('input[id^="checkbox_"]').not('#checkbox_all').click(function () {
            $('#checkbox_all').prop('checked', false);

   alert($(this).val());  

});​

http://jsfiddle.net/heten/

Working :)

I think you just need $(this).val(); in your click event.

Also, if you need to work with that value as an int later, use

var x = parseInt($(this).val(),10);

The val jQuery method should see you right.

$('#checkbox_all').val();

http://api.jquery.com/val/

发布评论

评论列表(0)

  1. 暂无评论