代码之家  ›  专栏  ›  技术社区  ›  ByteMyPixel

我如何在一个组中获得任意一个单选选项,以选择同级(隐藏)复选框?

  •  0
  • ByteMyPixel  · 技术社区  · 7 年前

    asked a similar question yesterday ,但这个场景有点不同,因为有几个无线电选项(可以是任意数量),我需要 任何一个 选择同级复选框(将隐藏)。

    编辑(更多信息):此外:可以有几个 <div class="item"> 在一个 <div class="panel-body">

    这是我的一把小提琴 到目前为止,只有最后一台收音机选中了复选框 :

    https://jsfiddle.net/ByteMyPixel/cqdj4jt6/1/
    更新的小提琴: https://jsfiddle.net/ByteMyPixel/cqdj4jt6/3/

    以下是html:

    <div class="panel-body rosette-body">
    
        <div class="item">
            <div class="the-options checkbox-wrapper">
    
                <div class="set checker">
                    <input id="green-abalone-ring" name="rosette_option" type="radio" value="Green Abalone Ring"> 
                    <label for="green-abalone-ring">Green Abalone Ring</label>
                </div>
    
                <div class="set checker">
                    <input id="bloodwood-ring" name="rosette_option" type="radio" value="Bloodwood Ring"> 
                    <label for="bloodwood-ring">Bloodwood Ring</label>
                </div>
    
                <div class="set checker">
                    <input id="koa-wood-ring" name="rosette_option" type="radio" value="Koa Wood Ring"> 
                    <label for="koa-wood-ring">Koa Wood Ring</label>
                </div>
    
                <div class="hidden-set">
                    <input name="rosette_title" type="checkbox" value="Simple Rosette example">
                    <input name="rosette_img" type="checkbox" value="/images/uploads/main/simple-rosette.jpg">
                </div>
            </div>
        </div><!-- [END] item -->
    
    </div>
    

    我尝试使用的jQuery:

    $(document).on('change', '.rosette-body .checker input[type="radio"]', function() {
      $(this)
        .parents('.panel-body.rosette-body')
        .find('.checker')
        .each(function() {
          //var checkboxes = $(this).siblings('input[type="checkbox"]'),
          var checkboxes = $(this).siblings('.hidden-set').find('input[type="checkbox"]'),
            radio = $('input[type="radio"]', this).get(0);
    
          checkboxes.prop('checked', radio.checked);
        });
    });
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Snowmonkey    7 年前

    我建议你不要费心去重复所有的单选按钮,真的没有必要。您可能只需要当前选定单选按钮的值,以确定如何处理隐藏的复选框。如果是这样,下面的代码就是这样做的。最后一行实际实例化了我的RadioToCheckboxWidget,它用适当的监听器包装了给定的元素。当用户选择单选按钮时,我们得到所选单选按钮的值(在我的示例中为值或“”。基于该值,我们遍历复选框并将其设置为实际值。

    把这当成一把小提琴, here you go .

    /*****
     * RadioToCheckboxWidget(jQueryDOMNode)
     *   The RadioToCheckboxWidget takes a collection of radio button els
     *   and a collection of checkbox els, then toggles the checkbox based
     *   on the status of the radio buttons. if the radio with NO value is
     *   selected, the checkboxes are unchecked. If any other radio button
     *   is selected, then the checkboxes are checked.
     *
     *****/
    var RadioToCheckboxWidget = function(element) {
      this.el = element;
      this.__init();
    };
    
    $.extend(RadioToCheckboxWidget.prototype, {
      __init: function() {
        /***
         * This function was just edited, to allow the user to pass
         *  a collection of nodes. Thus, the user can either apply it
         *  one-by-one like this:
         *     var myRadio = new RadioToCheckboxWidget($("#myID") );
         *  ... or it can be applied to a collection of nodes like:
         *     var myRadios = new RadioToCheckboxWidget($(".item") );
         *
         *  The second scenario above is how I've applied it, but by doing
         *   the rewrite, there is flexibility.
         *
         ***/
         
         // iterate over every node in the jQuery collection, and
         //  set up each one as its own widget with its own listeners.
         //  by doing this, selections in one have no affect on others.
          this.el.each(function() {
            // convenience -- create a reference to the current el.
            var el = $(this);
            // references to the radio and checkbox els.
            var radioBtnEls = el.find(".checker");
            var checkboxEls = el.find(".hidden-set input[type=checkbox]");
            
            // listen for changes to ANY of the radio buttons
            el.on("change", radioBtnEls, function() {
              // if the radio has a value (so it's not 'None'), 
              //  we check the boxes. Otherwise, we UNcheck them.
                var toggleVal = $(this).find("input[type=radio]:checked").val();
                checkboxEls.each(function() {
                    if (toggleVal == "") {
                      $(this).prop("checked", false);
                    } else {
                      $(this).prop("checked", true);
                    } // end if toggleVal
                  }) // end checkboxEls.each()
              }) // end on change
          });
        } // end init()
    
    });
    
    
    // This is the line that actually uses all the above code -- this
    //  one line creates as many widgets as I have .item nodes.
    var myRadioPanes = new RadioToCheckboxWidget($(".item"));
    .item {
      border: 1px dotted #ccc;
      display: inline-box;
      float: left;
      padding: 10px;
      margin: 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="panel-body rosette-body">
    
      <fieldset class="item">
      <legend>
      Wooden ring:
      </legend>
      <div class="the-options checkbox-wrapper">
    
          <div class="set checker">
            
            <label><input name="rosette_option" type="radio" checked value=""> None</label>
          </div>
          <div class="set checker">
            <input id="green-abalone-ring" name="rosette_option" type="radio" value="Green Abalone Ring">
            <label for="green-abalone-ring">Green Abalone Ring</label>
          </div>
    
          <div class="set checker">
            <input id="bloodwood-ring" name="rosette_option" type="radio" value="Bloodwood Ring">
            <label for="bloodwood-ring">Bloodwood Ring</label>
          </div>
    
          <div class="set checker">
            <input id="koa-wood-ring" name="rosette_option" type="radio" value="Koa Wood Ring">
            <label for="koa-wood-ring">Koa Wood Ring</label>
          </div>
    
          <div class="hidden-set">
            <input name="rosette_title" type="checkbox" value="Simple Rosette example">
            <input name="rosette_img" type="checkbox" value="/images/uploads/main/simple-rosette.jpg">
          </div>
        </div>
      </fieldset>
      <!-- [END] item -->
    
      <fieldset class="item">
      <legend>
      Gold chain necklace:
      </legend>
        <div class="the-options checkbox-wrapper">
    
          <div class="set checker">
            
            <label><input name="chain_option" type="radio" checked value=""> None</label>
          </div>
          <div class="set checker">
            <input id="three-strand-braid" name="chain_option" type="radio" value="Three-strand braid">
            <label for="three-strand-braid">Three-strand braid</label>
          </div>
    
          <div class="set checker">
            <input id="five-strand-braid" name="chain_option" type="radio" value="Five-strand gold braid">
            <label for="five-strand-braid">Five-strand braid</label>
          </div>
    
          <div class="set checker">
            <input id="seven-strand-braid" name="chain_option" type="radio" value="Seven-strand braid">
            <label for="seven-strand-braid">Seven-strand braid</label>
          </div>
    
          <div class="hidden-set">
            <input name="chain_title" type="checkbox" value="Simple Chain example">
            <input name="chain_img" type="checkbox" value="/images/uploads/main/simple-chain.jpg">
          </div>
        </div>
      </fieldset>
      <!-- [END] item -->
    
    </div>

    根据您的请求,您想知道如何将其用于多个 .item s、 一种可能的方法是,简单地对每一个都有一个硬引用 .项目 . 但这并不是一个很好的可扩展解决方案。相反,我重写了小部件的\uu init()函数,以便您可以传入一组jQuery节点,并将每个节点设置为一个独立的小部件。现在,您可以创建一组对特定节点的引用(如 new RadioToCheckBox($('#aSingleDOMNode') ); ),或者您可以创建对小部件的单个引用并传入整个集合(如 var myCollectionOfItems = new RadioToCheckbox($(".items") ); ).