简体   繁体   English

使用单选按钮显示div

[英]Use radio buttons to show divs

I want to show divs when i check one of the 2 radio buttons. 当我选中2个单选按钮之一时,我想显示div。 The problem is it always shows the divs at this point. 问题在于它始终会在此时显示div。

I am also working with cookies to store the choice when i refresh the page. 我也在刷新页面时使用Cookie来存储选择。 When i work with checkboxes both the divs and the cookies are working perfectly, besides the problem that it is possible to select both checkboxes. 当我使用复选框时,除可以同时选中两个复选框的问题外,div和cookie都工作正常。 That's why i choose to use radio buttons, but now the divs and the cookies both aren't working anymore. 这就是为什么我选择使用单选按钮的原因,但是现在div和cookie都不再起作用了。

<div class="check">
<p><input type="radio" value="Name" id="name" name="group[]" /> <label for="name">Name</label></p>  
<p><input type="radio" value="Reference " id="reference" name="group[]" /> <label for="reference">Reference</label></p></div>


<div id="nametxt"> Show Name TEXT </div>
<div id="referencetxt"> Show Ref TEXT </div>
<div id="referencetxt2"> Show Ref TEXT </div>

This is my jquery: 这是我的jQuery:

function getStorage(key_prefix) {
// this function will return us an object with a "set" and "get" method
// using either localStorage if available, or defaulting to document.cookie
if (window.localStorage) {
    // use localStorage:
    return {
        set: function(id, data) {
            localStorage.setItem(key_prefix+id, data);
        },
        get: function(id) {
            return localStorage.getItem(key_prefix+id);
        }
    };
} else {
    // use document.cookie:
    return {
        set: function(id, data) {
            document.cookie = key_prefix+id+'='+encodeURIComponent(data);
        },
        get: function(id, data) {
            var cookies = document.cookie, parsed = {};
            cookies.replace(/([^=]+)=([^;]*);?\s*/g, function(whole, key, value) {
                parsed[key] = unescape(value);
            });
            return parsed[key_prefix+id];
        }
    };
}}
 jQuery(function($) {
// a key must is used for the cookie/storage
var storedData = getStorage('com_mysite_checkboxes_'); 

$('div.check input:radio').bind('change',function(){        
    $('#'+this.id+'txt').toggle($(this).is(':checked'))
    $('#'+this.id+'txt2').toggle($(this).is(':checked'));
    // save the data on change
    storedData.set(this.id, $(this).is(':checked')?'checked':'not');
}).each(function() {
    // on load, set the value to what we read from storage:
    var val = storedData.get(this.id);
    if (val == 'checked') $(this).attr('checked', 'checked');
    if (val == 'not') $(this).removeAttr('checked');
    if (val) $(this).trigger('change');
});});

I have made a fiddle: http://jsfiddle.net/8kzwovkp/ 我做了一个小提琴: http : //jsfiddle.net/8kzwovkp/

You were using jQuery in your jsfiddle but hadn't added it. 您在jsfiddle中使用的是jQuery,但尚未添加。

EDITED: 编辑:

var storedData = getStorage('com_mysite_checkboxes_');

$('input[name="group[]"]').on('change', function () {
    if ($(this).val() == "Name") {
        $('#nametxt').show();
        $('#referencetxt').hide();
    } else {
        $('#nametxt').hide();
        $('#referencetxt').show();
    }
    $('input[name="group[]"]').each(function() {
        storedData.set(this.id, $(this).is(':checked') ? 'checked' : 'not');
    });
}).each(function() {
    var val = storedData.get(this.id);
    if (val == 'checked') {
        $(this).attr('checked', 'checked');
        $(this).trigger('change');
    }
    else if (val == 'not')
        $(this).removeAttr('checked');
});

jsfiddle DEMO jsfiddle演示

You could do something as simple as CSS visibility or opacity 0, but that wouldn't solve your cookie problem. 您可以做一些像CSS可见性或不透明度0一样简单的操作,但这不能解决cookie问题。 You'd have to let us have a look at your code for that. 您必须让我们看看您的代码。

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

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