简体   繁体   English

使用javascript将复选框转换为单选按钮?

[英]convert Checkbox into radio button using javascript?

I am trying to convert checkbox into radio button which is working partially , but not deselecting previous selected radio button.I am looking solution to show one button at a time as a selected.我正在尝试将复选框转换为部分工作的单选按钮,但没有取消选择先前选定的单选按钮。我正在寻找一次将一个按钮显示为选定按钮的解决方案。

var layers = {
    'Esri_WorldImagery': Esri_WorldImagery.addTo(this.baseMap),
    'World_Topo_Map': World_Topo_Map//.addTo(this.baseMap)
};

var layerHtml = '<ul class="fa-ul">';
for (var key in layers) {
    if (layers.hasOwnProperty(key)) {
        var state = this.baseMap.hasLayer(layers[key]) ? 'checked="checked"' : '';
        //layerHtml += '<li><label><input type="checkbox" ' + state + ' value="' + key + '" >' + key + '</label>';
        layerHtml += '<li><label><input type="radio" ' + state + ' value="' + key + '" >' + key + '</label>';
    }
}

layerHtml += '</ul>';
var widget = $('<div id="layer-control" class="sidebar-widget">' + layerHtml + '</div>');

widget.on('click', 'input[type="radio"]', function (e) {

    var was_Active = $(this).prop('checked');
    var value = $(this).val();
    if (was_Active) {
        layers[value].addTo(self.baseMap);
    }
    else {
        self.baseMap.removeLayer(layers[value]);
    }
});

First, regarding the current code with radio elements, as @Aswin Ramesh has told you, yo should add the name attribute.首先,关于带有radio元素的当前代码,正如@Aswin Ramesh告诉你的,你应该添加name属性。 From MDN :来自MDN

A radio group is defined by giving each of radio buttons in the group the same name.通过为组中的每个单选按钮赋予相同的名称来定义单选组。 Once a radio group is established, selecting any radio button in that group automatically deselects any currently-selected radio button in the same group.建立单选组后,选择该组中的任何单选按钮会自动取消选择同一组中的任何当前选择的单选按钮。

Besides the shape (circle vs square) that's the only difference between the radio and checkbox elements.除了形状(圆形与方形)之外,这是radiocheckbox元素之间的唯一区别。 So consider that checkboxes that behave like radio buttons might be confusing for the user.因此,考虑到行为类似于单选按钮的复选框可能会让用户感到困惑。

That said, if you really want to replicate that functionality on checkboxes, use JavaScript to deselect all the elements but the one which raised the click event.也就是说,如果您真的想在复选框上复制该功能,请使用 JavaScript 取消选择所有元素,但引发click事件的元素除外。

 $('#checkboxes').on('click', ':checkbox', function(e) { $('#checkboxes :checkbox').each(function() { if (this != e.target) $(this).prop('checked', false); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="radios"> <input type="radio" name="rOptions" value="radio1" checked> <input type="radio" name="rOptions" value="radio2"> <input type="radio" name="rOptions" value="radio3"> </div> <div id="checkboxes"> <input type="checkbox" value="checkbox1" checked> <input type="checkbox" value="checkbox2"> <input type="checkbox" value="checkbox3"> </div>

Note : you forgot to close the <li> tags.注意:您忘记关闭<li>标签。

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

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