简体   繁体   English

标签处于活动状态时如何选择收音机

[英]How to select radio when label has active class

I'm making a website where you can select a couple of options to create your own boat. 我正在建立一个网站,您可以在其中选择几个选项来创建自己的船。 I need to select radio buttons that are hidden behind a label, so in order to select them I'm writing some JS that does that exact thing. 我需要选择隐藏在标签后面的单选按钮,因此为了选择它们,我正在编写执行此功能的一些JS。

Here is a small amount of code that contains the particular problem (this code is used in the whole website). 这是包含特定问题的少量代码(此代码在整个网站中都使用)。

<div class="row specs">
    <div class="subtitle">Hull Colour</div>
    <div class="fourcol">
        <label class="item" id="white">
            <img src="images/8meter-outboard.jpg"> 
            <div class="price">Orange</div>
            <input class="form-input" data-color="white" name="color" type="radio" value="white" id="white_radio" />
        </label>
    </div>

    <div class="fourcol">
        <label class="item" id="Test">
            <img src="images/8meter-outboard.jpg"> 
            <div class="price">Grey</div>
            <input data-color="grey" name="color" type="radio" value="Test">
        </label>
    </div>

    <div class="fourcol last">
        <label class="item" id="orange">
            <img src="images/8meter-outboard.jpg"> 
            <div class="price">White</div>
            <input data-color="orange" name="color" type="radio" value="orange">
        </label>
    </div>
</div>

the JS: JS:

$('.item').on('click', function(e){
      e.preventDefault();
      $(this).toggleClass('done');
      document.getElementById('this').checked = true;
    });

Without my Javascript, this markup seems to work. 没有我的Javascript,此标记似乎可以正常工作。 I want to put the display of the <input> on none at the end 我想在最后不显示<input>的显示

You can utilize native HTML to achieve what you are trying to do. 您可以利用本机HTML来实现您想做的事情。 As Josh Sanger mentioned, when the for attribute is assigned to a label , a hook into the input for that label is instantiated automatically. 如Josh Sanger所述,当将for属性分配给label ,该标签input的钩子会自动实例化。 As long as the targeted input has a match id attribute it will be linked. 只要目标输入具有匹配id属性,它将被链接。 Once that hook has been made, you can use some basic jQuery selector mechanics to add classes to the HTML elements that you want appear differently. 制作完该钩子之后,您可以使用一些基本的jQuery选择器机制将类添加到希望以不同方式显示的HTML元素。

Make certain that the input tag for the label is within the label so that bound click events will properly trigger it. 请确保在input的标签label是内label ,这样绑定click事件将正确触发它。

 $('input[type="radio"]').on('click', function(e) { var colorDiv = $(this).closest($('div.fourcol')).find($('.price')); var removeSelected = $('div.fourcol').find($('.price').not($(this))); $(removeSelected).removeClass('done'); $(colorDiv).addClass('done'); }); 
 .orange, .gray, .white { width: 48px; height: 48px; font-size: .79em; text-align: center; } .orange { background-color: orange; border: solid 1px orange; } .gray { background-color: #CCC; border: solid 1px #CCC; } .white { background-color: #FFF; border: solid 1px #FFF; } input { float: left; display: inline-block; clear: none; } .row.specs { width: 250px; } .price.noColor { border: solid 1px #ACE; } .noColor { width: 48px; height: 48px; text-align: center; font-family: arial; font-size: 35px; color: red; } .fourcol { display: inline-block; vertical-align: top; font-family: arial; } .done { border: dashed 1px green; } .price.noColor.done { border: dashed 1px green; } body{ background-color: #DDE; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <div class="row specs"> <div class="subtitle">Hull Colour</div> <div class="fourcol"> <label class="item" for="orange_radio"> <div class="price orange">Orange</div> </label> <input data-color="orange" name="color" type="radio" value="orange" id="orange_radio" /> </div> <div class="fourcol"> <label class="item" for="grey_radio"> <div class="price gray">Grey</div> </label> <input data-color="grey" name="color" type="radio" value="gray" id="grey_radio" /> </div> <div class="fourcol"> <label class="item" for="white_radio"> <div class="price white">White</div> </label> <input data-color="white" name="color" type="radio" value="white" id="white_radio" /> </div> <div class="fourcol"> <label class="item" for="noColor_radio"> <div class="price noColor">X</div> </label> <input data-color="nocolor" name="color" type="radio" value="nocolor" id="noColor_radio" /> </div> </div> 

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

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