You have two issues in your code:
1) You have to use this
keyword to target the current check box and
2) You are using class symbol (.
) instead of id (#
);
$(document).ready(function() {
$('.chkbx').click(function() {
var selected = "";
$('.chkbx:checked').each(function() {
selected += $(this).attr('data-valuetwo') + ',';
});
selected = selected.substring(0, selected.length - 1);
$('#selecteditems').val(selected);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input chkbx" value="654321" data-valuetwo="Mike" id="customCheck1" name="choice[]">
<label class="custom-control-label" for="customCheck1">Mike</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input chkbx" value="654321" data-valuetwo="Jason" id="customCheck2" name="choice[]">
<label class="custom-control-label" for="customCheck2">Jason</label>
</div>
<div class="contents">
<h3>Options Selected: </h3>
<textarea id="selecteditems">
</textarea>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…