简体   繁体   中英

how to checked only one in one row checkbox using javascript

how to checked only one in one-row checkbox using javascript

here is my view

<?php $c=0; for($a=0; $a < 5; $a++) { ?>
<table>
<tr class="cbclass">
<td><input type="checkbox" name="cbname" value="1" id="cb" class="cb"></td>
<td><input type="text" name="f_nilai1[]" id="id_tnilai<?php echo $c++; ?>" ></td>

<td><input type="checkbox" name="cbname" value="1" id="cb" class="cb"></td>
<td><input type="text" name="f_nilai2[]" id="id_tnilai<?php echo $c++; ?>" ></td>

<td><input type="checkbox" name="cbname" value="1" id="cb" class="cb"></td>
<td><input type="text" name="f_nilai3[]" id="id_tnilai<?php echo $c++; ?>"></td>

<td><input type="checkbox" name="cbname" value="1" id="cb" class="cb"></td>
<td><input type="text" name="f_nilai4[]" id="id_tnilai<?php echo $c++; ?>"></td>

<td><input type="checkbox" name="cbname" value="1" id="cb" class="cb"></td>
<td><input type="text" name="f_nilai5[]" id="id_tnilai<?php echo $c++; ?>"></td>
</tr>

this is my javascript I have already changed value in input text when the checkbox is clicked, I use it because in insert_batch checkbox not post when null so I use input text to post it, and it's running well. next, is I want only checked in one row, can anybody help me??

 <script type="text/javascript"> $(document).ready(function() { var a = $(".cb").length; $(".cb").click(function(event) { for(var i=0; i<a; i++) { var check = document.getElementsByName('cbname'); if(check[i].checked) { $("#id_tnilai"+[i]).val(1); } else { $("#id_tnilai"+[i]).val(0); } } }); }); </script> 

You can find the silblings of the current checkbox and change the checked property of those silbilings to none. Code given below.

<script type="text/javascript">
$(document).ready(function() 
{
var a = $(".cb").length;                                            
  $(".cb").click(function(event)
  {
    var siblings = $(this).parent().siblings();
    for (var i = 0; i < siblings.length; i++) {
        $(siblings[i]).children().filter('input[type="checkbox"]').prop('checked','');
    }
    for(var i=0; i<a; i++) {
        var check = document.getElementsByName('cbname');
            if(check[i].checked) 
        {
            $("#id_tnilai"+[i]).val(1);
            } else {
            $("#id_tnilai"+[i]).val(0);
            }
        }

   });
});
</script>

Hope this will solve your problem. Thank you

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