简体   繁体   English

单击带有标签的单选按钮

[英]Radio button clicked with label

I have multiple radio buttons. 我有多个单选按钮。 In the code that i have they are different pictures corresponding with different functions. 在我拥有的代码中,它们是对应于不同功能的不同图片。

The user has to know which radio button is clicked. 用户必须知道单击了哪个单选按钮。 Therefor i'm trying to find a way to change the background of the radio button after it has been clicked. 因此,我试图找到一种方法来单击单选按钮的背景。 In order for the code to work the <label> has to stay arround the <input> tag. 为了使代码正常工作, <label>必须保留在<input>标记周围。

 <div class="radio-toolbar">

        <label class="BottomLeft">
            <input id="tech" type="radio" ng-model="SelectedLayout" value="BottomLeft" />
        </label>

        <label class="BottomRight">
            <input id="tech" type="radio" ng-model="SelectedLayout" value="BottomRight" />
        </label>

        <label class="Dual">
            <input id="tech" type="radio" ng-model="SelectedLayout" value="Dual" />
        </label>

        <label class="DualRight">
            <input id="tech" type="radio" ng-model="SelectedLayout" value="DualRight" />
        </label>

        <label class="Duplex">
            <input id="tech" type="radio" ng-model="SelectedLayout" value="Duplex" />
        </label>

        <label class="Custom">
            <input id="tech" type="radio" ng-model="SelectedLayout" value="Custom" />
        </label>
    </div>

Here's the JSfiddle 这是JSfiddle

You can try this: 您可以尝试以下方法:

$('input[type=radio]')
    .on('click', function() {
        var $label = $(this).closest('label');
        $('label').not($label).css('background-color', 'green');
        $label.css('background-color', '#2C8BDE');
});

Here is the FIDDLE . 这是FIDDLE

Also, you must have unique ID 's in html. 另外,您必须在html中具有唯一ID

To work your labels , your code should look like this.The problem is that , you cannot have same ID for all elements. 要使用标签,您的代码应如下所示。问题是,您不能为所有元素都使用相同的ID。 ID is unique and if you want to work with label , you have to put for="#" attribute in your <label></label> element.So , remember <label></label> goes with for="" attribute , and that for="" attribute works with the id in the <input /> tag.And I've made your radio buttons non-multiple , so remove name="" attribute to work as multiple. ID是唯一的,如果要使用label,则必须在<label></label>元素中添加for="#"属性。因此,请记住<label></label>for=""属性一起使用,并且for=""属性与<input />标记中的id配合使用。我已将您的单选按钮设为非多个,因此请删除name=""属性以使其成为多个。

  <div class="radio-toolbar">

            <label for="first">
                Tech<input id="first" type="radio" ng-model="SelectedLayout" name="radioButton"  value="BottomLeft" />
            </label>

            <label for="second">
                AnotherOne<input id="second" type="radio" ng-model="SelectedLayout" name="radioButton" value="BottomRight" />
            </label>

            <label for="third">
                Third<input id="third" type="radio" ng-model="SelectedLayout"  name="radioButton" value="Dual" />
            </label>

            <label for="fourth">
                Fourth<input id="fourth" type="radio" ng-model="SelectedLayout" name="radioButton" value="DualRight" />
            </label>

            <label for="fifth">
                Fifth<input id="fifth" type="radio" ng-model="SelectedLayout" name="radioButton" value="Duplex" />
            </label>

            <label for="sixth">
                Sixth<input id="sixth" type="radio" ng-model="SelectedLayout" name="radioButton" value="Custom" />
            </label>
        </div>

Try this .I have changed backgorund color checked radio button to orange with jquery 试试这个 。我已经用jQuery将backgorund颜色选中的单选按钮更改为橙色

 $('input[type=radio]')
    .on('click', function() {
        var $label = $(this).closest('label');
        $('label').not($label).css('background-color', 'green');
        $label.css('background-color', '#2C8BDE');
});

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

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