[英]Get the value of the data attribute when an element is clicked on using jQuery?
[英]Get value attribute of the clicked checkbox via JQuery
我有這個checkbox
,其值為1 。
<input type="checkbox" name="option_1" id="checkbox_1" value="1">
我也使用這種方法來檢查/取消選中它。
$('input[id^="checkbox_"]').not('#checkbox_all').click(function () {
$('#checkbox_all').prop('checked', false);
// Get 'VALUE' of the checkbox here
});
我需要的是以某種方式得到點擊checkbox
'VALUE'。 所以在那種情況下它應該是1 。
有什么線索可以做到嗎?
謝謝!
在您的單擊方法中,使用此方法獲取值
$(this).attr("value");
$(this)
引用已單擊的對象。
編輯:你也可以使用$(this).val();
但有時候我對IE的老版本有問題,所以我確實首先推薦$(this).attr("value")
。
<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());
});
工作:)
我想你只需要$(this).val();
在您的點擊事件中。
此外,如果您以后需要將該值作為int使用,請使用
var x = parseInt($(this).val(),10);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.