Javascript 通过 JQuery 获取被点击复选框的值属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11952565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:47:43  来源:igfitidea点击:

Get value attribute of the clicked checkbox via JQuery

javascriptjquery

提问by Developer

I have got this checkboxwhich has value 1.

我有这个checkbox1

<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.

我需要的是以某种方式获得 clicked 的“VALUE” checkbox。所以在那种情况下它应该是1

Any clue how do it could be done?

任何线索如何做到?

Thank you!

谢谢!

回答by mas-designs

In your click method use this to get the value

在您的点击方法中,使用它来获取值

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

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

$(this)正在引用已单击的对象。

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.

编辑:你也可以使用,$(this).val();但有时我在使用旧版本的 IE 时遇到问题,所以我$(this).attr("value")首先推荐。

回答by LmC

?<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/

http://jsfiddle.net/heten/

Working :)

在职的 :)

回答by Gil Zumbrunnen

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

我认为你只需要$(this).val();在你的点击事件中。

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

此外,如果您稍后需要将该值作为 int 使用,请使用

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

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

回答by Robin Whittleton

The valjQuery method should see you right.

valjQuery的方法应该能看到你的权利。

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

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

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