Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

javascript - Display the selected value of checkbox in a div

I have two checkboxes and a div in the body and I would like to display the selected value of data-valuetwo inside the div, "contents".

$(document).ready(function() {
    $('.chkbx').click(function() {
        var selected = "";
        $('.chkbx:checked').each(function() {
            selected += $('.chkbx').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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...