【问题标题】:Javascript/JQuery setting value of checkbox while disabling othersJavascript/JQuery 设置复选框的值,同时禁用其他复选框
【发布时间】:2015-12-17 22:32:16
【问题描述】:

我正试图弄清楚一些事情。我有几个复选框,采用以下形式

<div class="checkbox">
  <input name="someName1" class="checkbox styled" id="checkbox1" onclick="document.controller.setValue('/@someName1', this.checked ? '1' : '', 'someName1');" type="checkbox" value="" />
  <label for="checkbox1"> SomeName 1 </label>
</div>

<div class="checkbox">
  <input name="someName2" class="checkbox styled" id="checkbox2" onclick="document.controller.setValue('/@someName2', this.checked ? '1' : '', 'someName2');" type="checkbox" value="" />
  <label for="checkbox2"> SomeName 2 </label>
</div>

需要控制器来设置我正在使用的某些系统中的值。现在以上似乎工作,如果我检查两者,它们都记录在我的系统中。如果我选中一个然后取消选中它,它不会被记录。

问题来了,因为我只希望他们能够选择一个选项(我知道单选按钮是为此,但我需要使用复选框)。

所以我有以下

$('input.styled').on('change', function() {
    $('input.styled').not(this).prop('checked', false);  
});

这样可以确保只选择一个。但是,由于 onclick 事件,如果我选中一个复选框并选中一个不同的复选框(然后上面的代码会关闭第一个复选框),这两个复选框都被记录为在我的系统中被选中。

因此,我认为我需要对我的 onclick 事件做一些事情,如果不再选中复选框,以某种方式将其关闭。

我正在考虑做类似以下的事情

$('.checkbox').change(function() {
    if($(this).is(":checked")) {
        $value = $(this).attr("name");
       document.controller.setValue('/@'+$value, this.checked ? '1' : '', 'checkbox');
    }   
});

因此,如果复选框被选中,请将 setValue 函数应用于名称属性已选中的复选框。如果未选中,我需要以某种方式扭转这一点。

我将如何去做这样的事情?

谢谢

【问题讨论】:

    标签: javascript jquery checkbox


    【解决方案1】:

    你可以改变这个

       $('.checkbox').change(function() {
            if($(this).is(":checked")) {
                $value = $(this).attr("name");
               document.controller.setValue('/@'+$value, this.checked ? '1' : '', 'checkbox');
            }   
        });
    

    到这里

        $('.checkbox').change(function() {
          $(".checkbox:checkbox:not(:checked)").each(function( index ) {
            $value = $(this).attr("name");
            document.controller.setValue('/@'+$value,'', 'checkbox');
          });
          if($(this).is(":checked")) {
            $value = $(this).attr("name");
            document.controller.setValue('/@'+$value, this.checked ? '1' : '', 'checkbox');
          }   
        });
    

    在为选中项设置值之前,您将重置所有未选中项。

    【讨论】:

      【解决方案2】:

      我认为这是你需要的

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <script>
      $(document).ready(function() {
      
        checkbox2radio('.checkbox');
        checkbox2radio('.hamlet');
      
        function checkbox2radio(selector) {
          $(selector).on('change', function(e) {
            var checked = this.checked;
            var index = $(selector).index(this);
            if (checked) {
              $(selector).each(function(i) {
                if(i == index) {
                }
                else {
                  $(selector).eq(i).removeAttr('checked');
                }
              });
            }
          });
        }
      
      })
      </script>
      <input type="checkbox" class="checkbox"> Foo<br>
      <input type="checkbox" class="checkbox"> Bar<br>
      <input type="checkbox" class="checkbox"> Hello<br>
      <input type="checkbox" class="checkbox"> World<br>
      <hr>
      <input type="checkbox" class="hamlet"> to<br>
      <input type="checkbox" class="hamlet"> be<br>
      <input type="checkbox" class="hamlet"> or<br>
      <input type="checkbox" class="hamlet"> not<br>
      <input type="checkbox" class="hamlet"> to<br>
      <input type="checkbox" class="hamlet"> be<br>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-05
        • 1970-01-01
        • 2011-05-24
        相关资源
        最近更新 更多