我想从课堂上获得价值,并定义了价值
<input type="checkbox" class="cat" value="3">
<input type="checkbox" class="cat" value="5">
<input type="checkbox" class="cat" value="8">
和我的jQuery代码
$('.cat').click(function(){
var cat = Number($('.cat').val());
$(".cat").val(cat);
// after that using my code
}
在选择器中删除$(".cat").val(cat);
并使用this
而不是'.cat'
$('.cat').click(function(){
var cat = Number($(this).val());
// after that using my code
});
$(function(){
$('.cat').click(function(){
var cat = Number($(this).val());
console.log("cat value: " + cat);
// after that using my code
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="cat" value="3">
<input type="checkbox" class="cat" value="5">
<input type="checkbox" class="cat" value="8">
它的工作,但当我单击它们时,赋予其他复选框sam值
您的代码正常工作,我想我有代码问题,谢谢