Home > Software design >  How to label value of unchecked checkboxes with jquery?
How to label value of unchecked checkboxes with jquery?

Time:11-18

I have multiple checkboxes with label, i want to get label of unchecked checkboxes and send these label values to php file when form is submitted. I'm doing this but didn't work

$('.checkbox_0').click(function() {
  $("input:checkbox:not(:checked)").each(function() {
    $(this).next('label').text()
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="firstCheckbox" name="firstCheckbox" class="checked_0">
<label for="firstCheckbox">Attendance to shifts are regular and no last minute shift cancellation</label>

<input type="checkbox" id="secondCheckbox" name="secondCheckbox" class="checked_0">
<label for="secondCheckbox">Attendance to shifts are regular and no last minute shift cancellation</label>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can save the uncheck label in a separate array as below.

$(document).ready(function(){
var unCheckedLabelText=[];
  $('.checkboxClass').click(function () {
     unCheckedLabelText=[];
        $("input:checkbox:not(:checked)").each(function(){
            var text=$(this).next('label').text();
            unCheckedLabelText.push(text);
        });
    });
});

CodePudding user response:

Just use the right class selector and push it into an array.

How to send it to PHP depends on your implementation.

$('.checked_0').click(function() {
  let result = [];
  $("input:checkbox:not(:checked)").each(function() {
    result.push($(this).next('label').text());
  });
  console.log(result);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="firstCheckbox" name="firstCheckbox" class="checked_0">
<label for="firstCheckbox">Attendance to shifts are regular and no last minute shift cancellation</label>

<input type="checkbox" id="secondCheckbox" name="secondCheckbox" class="checked_0">
<label for="secondCheckbox">Attendance to shifts are regular and no last minute shift cancellation</label>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related