简体   繁体   English

使用基于已选中/未选中的单选按钮的自定义数据属性显示/隐藏容器

[英]Showing/Hiding a container using custom data-attribute based on a radio button that's checked/unchecked

在此处输入图片说明

Please take a look at the diagram above first. 请先看上图。

The purpose of this task is to be able to show/hide a container using custom data-attribute and whether a radio button is check/unchecked. 该任务的目的是能够使用自定义数据属性显示/隐藏容器,以及是否选中/取消选中单选按钮。

So here is how we use it; 所以这是我们的用法

<ul class="radio group">
<li><input name="radio-1" type="radio" value="1">Motorbike</li>
<li><input name="radio-1" type="radio" value="2">Car</li>
<!-- THIS IS OUR TRIGGER -->
<li><input name="radio-1" type="radio" value="3" data-toggle="expand" data-toggle-target=".container">Other</li>
</ul>

<!-- THIS SHOULD BE HIDDEN, IF NOT THEN WE SET IT TO BE HIDDEN ON PAGE LOAD -->
<div class="container">
<!-- SOME CONTEXT -->
</div>

The code above showcases how do we use the custom data-attribute within our HTML markup to perform this, and it works when you click on the desired radio button, but when clicking on other radio buttons, the active one should be unchecked as we have clicked on the other radio buttons, and then the container should be displayed out, but it's not fully working. 上面的代码展示了我们如何使用HTML标记中的自定义数据属性来执行此操作,当您单击所需的单选按钮时,它可以工作,但是当单击其他单选按钮时,应该像我们一样取消选中活动的属性单击其他单选按钮,然后应该将容器显示出来,但不能完全正常工作。

Please take a look at my jsFiddle version! 请看看我的jsFiddle版本!

Any help is greatly appreciated :) 任何帮助是极大的赞赏 :)

Here is a solution... 这是一个解决方案...

https://jsfiddle.net/Farzad/xtwquh7a/ https://jsfiddle.net/Farzad/xtwquh7a/

And here is partial of the entire code that has changed; 这是已更改的全部代码的一部分;

// IF RADIO BUTTON
  if (_triggerType == 'radio') {

                $('[name="' + _$trigger.attr('name') + '"]').each(function () {

        if ($(this).val() != _$trigger.val())
        {
                        $(this).change(function(event, source){
            if(source != _$trigger.val())
                if(source === undefined)
                _$trigger.trigger("change", _$trigger.val());
              else  
                _$trigger.trigger("change", source);    
          })
        }
      });

    _$trigger.change(function () {
      // CHECKED
      if (this.checked) {
        expandIt(_$target);

        // UNCHECKED
      } else if (!this.checked) {
        collapseIt(_$target);
      }
    });
    }

After digging and spending a lot of time, finally friend of mine Mihir Solanki came with a solution which works absolutely perfect! 在花费大量时间后,我的朋友Mihir Solanki终于提出了绝对完美的解决方案! Again big thank you Mihir :) 再次感谢米希尔:)

Hope this helps others too that may have come across similar tasks before! 希望这也可以帮助其他以前可能遇到过类似任务的人!

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM