【问题标题】:How to label value of unchecked checkboxes with jquery?如何用jquery标记未选中复选框的值?
【发布时间】:2021-12-29 02:19:36
【问题描述】:

我有多个带有标签的复选框,我想获取未选中复选框的标签,并在提交表单时将这些标签值发送到 php 文件。 我正在这样做,但没有工作

$('.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>

【问题讨论】:

  • 请分享您的html代码。
  • 问题已更新,请立即查看html代码
  • 代码中不存在带有“checkbox_0”的类名。从哪里获得?
  • 而你的函数什么也没做。
  • @RajuAhmed 请再次检查

标签: php jquery


【解决方案1】:

您可以将取消选中的标签保存在单独的数组中,如下所示。

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

【讨论】:

    【解决方案2】:

    只需使用正确的类选择器并将其推入数组即可。

    如何将其发送到 PHP 取决于您的实现。

    $('.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>

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2016-04-26
      • 2015-01-31
      • 2013-09-15
      • 1970-01-01
      • 2011-04-14
      • 2012-06-29
      • 2014-04-28
      • 2013-01-23
      相关资源
      最近更新 更多