【问题标题】:How to get the exact unique value on checkBox select while other checkBox is deselected? (jQuery)如何在取消选择其他复选框时获得复选框选择的确切唯一值? (jQuery)
【发布时间】:2021-12-27 17:12:33
【问题描述】:

我创建了一组复选框,我的目标是选择一个复选框并取消选中另一个复选框。

我已经使用 Stackoverflow 的解决方案成功地实现了这一点。

现在,我正在尝试获取选中复选框的值。这里的主要问题是,当我选择一个复选框时,有时我会得到以前的值而不是当前值。

示例 - 当我单击带有标签 5 的复选框时,我得到 5。然后,我单击 10 有时我得到 10,但通常它显示 5。当我进入下一个 15 时,我之前的值显示为 10 和以此类推。

  $("form :input").change(function () {
    var checkboxes = $("input[name='" + this.name + "']:checked");
    console.log(checkboxes.val());
    if (checkboxes) {
      $(checkboxes).not(this).prop("checked", false);
     }
     
});
});
<form action="#" method="post">
        <div class="cal_box">
          <div class="part1">
            

            <div class="bill_input">
              <label for="custom_tip">Select Tip %</label>
              <div class="tip_btn_group">
                <input type="checkbox" name="tip" id="tip5" value="5" />
                <label class="btn_box" for="tip5">5%</label>
                <input type="checkbox" name="tip" id="tip10" value="10" />
                <label class="btn_box" for="tip10">10%</label>
                <input type="checkbox" name="tip" id="tip15" value="15" />
                <label class="btn_box" for="tip15">15%</label>
                <input type="checkbox" name="tip" id="tip25" value="25" />
                <label class="btn_box" for="tip25">25%</label>
                <input type="checkbox" name="tip" id="tip50" value="50" />
                <label class="btn_box" for="tip50">50%</label>
                <input
                  type="text"
                  name="custom_tip"
                  id="tip"
                  value=""
                  placeholder="Custom"
                />
              </div>
            </div>

          
          </div>
        </div>
      </form>

https://jsfiddle.net/mrrajsoni/run821g5/3/

【问题讨论】:

  • 您可能需要考虑切换到单选按钮。这可以设置为允许用户仅选择列表中的一项。复选框默认允许用户选中多个项目。
  • UX:您可能希望在“custom_tip”输入之前有另一个复选框来明确设置它自定义 - 否则您的用户可以同时勾选tip5 custom_tip - 应该是? (我想您可以像清除复选框一样清除输入)
  • 您的小提琴,提供的 (/3/) 缺少 }) 所以无法运行。
  • @Twisty - 我使用复选框是因为我将有一个重置按钮来重置值并且不想保留任何默认值,这就是为什么不使用单选按钮的原因。
  • Updated fiddle 使用带有重置按钮的收音机。只是为了完整性。

标签: jquery checkbox


【解决方案1】:

您的问题是:第二次点击 $("input[name='" + this.name + "']:checked") 包含 2 复选框(请参阅 console.log(checkboxes.length))但是:

  • .val() 将始终只给出第一个的值。

所以勾选 15,然后勾选 25,.val() 给出 15。
勾选 25,然后勾选 15,.val() 给出 15(因为它的索引较低)

修复是使用this获取当前复选框:

$(function() {
  $("form :input").change(function() {
    $("input[name='" + this.name + "']:checked").not(this).prop("checked", false);  
    console.log($(this).is(":checked") ? $(this).val() : "none")
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
  <label for="custom_tip">Select Tip %</label>
  <div class="tip_btn_group">
    <input type="checkbox" name="tip" id="tip5" value="5" />
    <label class="btn_box" for="tip5">5%</label>
    <input type="checkbox" name="tip" id="tip10" value="10" />
    <label class="btn_box" for="tip10">10%</label>
    <input type="checkbox" name="tip" id="tip15" value="15" />
    <label class="btn_box" for="tip15">15%</label>
    <input type="checkbox" name="tip" id="tip25" value="25" />
    <label class="btn_box" for="tip25">25%</label>
    <input type="checkbox" name="tip" id="tip50" value="50" />
    <label class="btn_box" for="tip50">50%</label>
    <input type="text" name="custom_tip" id="tip" value="" placeholder="Custom" />
  </div>
</form>

【讨论】:

  • 非常感谢您的澄清和解释性回答。您还清除了我的第二个疑问,即为什么选择 50 给我 50,然后选择 25 给我 25,而不是反过来。我将详细了解“this”关键字。
【解决方案2】:

一句话,你遇到了这个错误因为你是在刷新其他复选框之前你的复选框的日志记录值。 解决这个问题只需放

console.log(checkboxes.val());

在你之后

if (checkboxes) {
      $(checkboxes).not(this).prop("checked", false);
}

它肯定会解决你的问题。


另外:

  1. 我建议使用单选按钮而不是复选框 它最准确地符合要求,即用户必须能够 仅选择 1 个选项。如果纯粹是因为审美选择 给你。
  2. 还有一个书呆子评论,即使不更改使用console.log(this.value) 而不是console.log(checkboxes.val()) 的流代码,您仍然可以输出正确的值

【讨论】:

  • 这是正确的答案(“复选框的记录值[es]”)(所以 +1'd),但是,简单地将 console.log(checkboxes.val()); 移动到末尾不会产生预期的效果,因为checkboxes 已经设置并且 jquery 不执行“实时”查询。您需要重新查询(按照您的建议使用this 更容易)。
【解决方案3】:

考虑以下内容。

$(function() {
  $(".tip_btn_group input[type='checkbox']").change(function(event) {
    var $self = $(this);
    var tip;
    $(".tip_btn_group input[type='checkbox']").not($self).prop("checked", false);
    if ($(".tip_btn_group input[type='checkbox']:checked").length == 0) {
      tip = 0;
    } else {
      tip = parseInt($self.val());
    }
    console.log(tip);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="#" method="post">
  <div class="cal_box">
    <div class="part1">
      <div class="bill_input">
        <label for="custom_tip">Select Tip %</label>
        <div class="tip_btn_group">
          <input type="checkbox" name="tip" id="tip5" value="5" />
          <label class="btn_box" for="tip5">5%</label>
          <input type="checkbox" name="tip" id="tip10" value="10" />
          <label class="btn_box" for="tip10">10%</label>
          <input type="checkbox" name="tip" id="tip15" value="15" />
          <label class="btn_box" for="tip15">15%</label>
          <input type="checkbox" name="tip" id="tip25" value="25" />
          <label class="btn_box" for="tip25">25%</label>
          <input type="checkbox" name="tip" id="tip50" value="50" />
          <label class="btn_box" for="tip50">50%</label>
          <input type="text" name="custom_tip" id="tip" value="" placeholder="Custom" />
        </div>
      </div>
    </div>
  </div>
</form>

这实际上取消选中任何其他复选框,以确保在任何时候都只选中一个。用户也可以取消选中他们的选择,您需要为自定义条目添加额外的代码。

【讨论】:

  • 感谢 Twisty 的解决方案。它工作得非常好。但是我接受了 freen-m 的答案,因为我已经有了完全相同的代码并且只需要修改一个单词。再次感谢您!
【解决方案4】:

这是你的表格

<form action="#" method="post">
    <div class="cal_box">
      <div class="part1">
        

        <div class="bill_input">
          <label for="custom_tip">Select Tip %</label>
          <div class="tip_btn_group">
            <input type="checkbox" name="tip" id="tip5" value="5" />
            <label class="btn_box" for="tip5">5%</label>
            <input type="checkbox" name="tip" id="tip10" value="10" />
            <label class="btn_box" for="tip10">10%</label>
            <input type="checkbox" name="tip" id="tip15" value="15" />
            <label class="btn_box" for="tip15">15%</label>
            <input type="checkbox" name="tip" id="tip25" value="25" />
            <label class="btn_box" for="tip25">25%</label>
            <input type="checkbox" name="tip" id="tip50" value="50" />
            <label class="btn_box" for="tip50">50%</label>
            <input
              type="text"
              name="custom_tip"
              id="tip"
              value=""
              placeholder="Custom"
            />
          </div>
        </div>

      
      </div>
    </div>
  </form>

这是你的 JS 代码

$('input[type=checkbox]').on('change',function(){
    var box = $("input[name = "+this.name+"]"+":checked").val();
    if(box){

    console.log(box);
    }
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2012-04-22
    • 2015-05-08
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多