简体   繁体   中英

How can I target unchecked checkboxes in jQuery?

When I uncheck any checkbox it also appends the result. I want to remove that. What should I do?

$(document).ready(function(){
    $('.chk_val').on('click',function(){
        var result = $(this).val();
        $('#course').append(result);
    });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="show">
<span id="course"> </span>
</div>

<input type="checkbox" class="chk_val" value="20" />20
<br />
<input type="checkbox" class="chk_val" value="30" />30
<br />
<input type="checkbox" class="chk_val" value="40" />40
<br />

 $(document).ready(function() { $('.chk_val').on('click', function() { var result = $('.chk_val:checked').map(function() { return $(this).val() }).get(); $('#course').html(result); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="show"> <span id="course"> </span> </div> <input type="checkbox" class="chk_val" value="20" />20 <br /> <input type="checkbox" class="chk_val" value="30" />30 <br /> <input type="checkbox" class="chk_val" value="40" />40 <br /> 

  1. Map the value of checked items.
  2. Use .html() to change the html of the div

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM