简体   繁体   English

如何在JavaScript中检查/取消选中动态单选按钮?

[英]How to Check/uncheck dynamic radio buttons in javascript?

This question is asked probably a million times but I can't get this one to work. 这个问题大概被问了一百万遍,但我无法解决这个问题。

I have 3 radio buttons which are created normally. 我有3个正常创建的单选按钮。 I want to place a 4th radio button between the first and third radio button. 我想在第一个和第三个单选按钮之间放置第四个单选按钮。 When clicking this radio button the third and fourth radio button should appear and unchecking this radio should make them disappear. 单击此单选按钮时,第三个和第四个单选按钮应出现,并且取消选中此单选按钮应使它们消失。

To clarify I created a fiddle 为了澄清我创建了一个小提琴

So what I tried is below. 所以我尝试的是下面。 I created a new div with the radio. 我用收音机创建了一个新的div。 Injected it after the first radio. 在第一台收音机后注入。 This works ok! 这样行得通! When I check the newly created radio the 3rd and 4th appear. 当我检查新创建的收音机时,将出现第三和第四。 So works ok! 这样工作就可以了! The problem is when I click the first radio, the second keeps checked so the 3rd and 4th radio is are still visible. 问题是当我单击第一个单选按钮时,第二个单选按钮保持选中状态,因此第三个和第四个单选按钮仍然可见。 I thought that it would work with .prop or I'm obviously doing something wrong. 我以为它可以与.prop一起.prop否则我显然做错了。

Anybody? 有人吗

$(document).ready(function () {
    var newContainer = $('<div class="gui-spacer"></div><div class="gui-block-option"><div class="gui-field"><div class="gui-radio"><div class="tm-radio"><input type="radio" value="" name="shipment_method_wholesale" id="gui-form-shipping-wholesale" class=""></div><label for="gui-form-shipping-wholesale">Ik wil graag bij mijn groothandel bestellen(Afhalen)</label></div><div class="gui-field-content"><p>text over bestellen bij groothandel</p></div></div></div>');

    $('#gui-form .gui-block-option:not(:first)').hide();
    $('#gui-form-shipping-wholesale').prop('checked', false);
    $('#gui-form .gui-block-option:first').after(newContainer);


    $('input[name="shipment_method_wholesale"]').bind('change', function () {

        var showOrHide = ($(this).val() == 1) ? false : true;
        $('#gui-form .gui-block-option:not(:first) ').toggle(showOrHide);
        $(this).prop('checked', true);
    });
});

Radio buttons are grouped by their name property. 单选按钮按其name属性分组。 If you want to ensure that only one can be selected at a time then make sure that they have the same name . 如果要确保一次只能选择一个,请确保它们具有相同的name

All I did to get your jsfiddle to work was change the name of the radio button you were dynamically adding to shipment_method . 我所做的一切,让您的jsfiddle工作是改变name的单选按钮进行动态增加的shipment_method

var newContainer = $('<div class="gui-spacer"></div><div class="gui-block-option"><div class="gui-field"><div class="gui-radio"><div class="tm-radio"><input type="radio" value="" name="shipment_method" id="gui-form-shipping-wholesale" class=""></div><label for="gui-form-shipping-wholesale">Ik wil graag bij mijn groothandel bestellen(Afhalen)</label></div><div class="gui-field-content"><p>text over bestellen bij groothandel</p></div></div></div>');

Edit: I had to make some other changes to get it hiding and displaying the other buttons correctly 编辑:我必须进行一些其他更改以使其隐藏并正确显示其他按钮

要成为同一组的一部分,您需要使该组中所有按钮的“名称”属性相同。

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

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