简体   繁体   中英

Show/Hide div using jQuery and jQueryUI

I'm very new to this and have limited knowledge (all self taught over the last few weeks). I have tried a number of solutions from similar questions that others have asked, and none seem to work for me.

I am trying to set the 'date_range' div to only be visible when the 'range' filter is selected. I'm using jQuery 1.8.2 and jQueryUI 1.9 (set with $.noConflict(); ).

My code is below (please be kind, I know I'm not very good at this), any help is appreciated.

<script>
$('#filters').is(function() {
if ($('#range').attr('checked')) {
    $('#date_range').show();
} else {
    $('#date_range').hide();
}
});
</script>

    <div>
        <form align="center" id="filters">
            <input type="radio" id="last24" name="filter_radio" checked="checked"/><label for="last24">Last 24hrs</label>
            <input type="radio" id="WTD" name="filter_radio" /><label for="WTD">WTD</label>
            <input type="radio" id="last_week" name="filter_radio" /><label for="last_week">Last Week</label>
            <input type="radio" id="range" name="filter_radio" /><label for="range">Range</label>
                <div id="date_range">
                    <br />
            <label for="from">From: </label><input type="text" id="from" name="from"/>
            <label for="to">To: </label><input type="text" id="to" name="to" />
                </div>
        </form>     
    </div>

Try this code

$(function(){

    $('#date_range').hide();

    $('#filters').on('click' ,'input[type=radio]', function(){
        if (this.id == "range" && this.checked) {
            $('#date_range').show();
        } else {
            $('#date_range').hide();
        }
    });

});

Check this DEMO

Hope it will help

Try this code:-

This is jquery to hide and show:-

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js" ></script>
<script>
function asfd(str)
{
if(str==true)
{
jQuery('div#renew').show();
}
else
{
jQuery('div#renew').hide();
}
}
</script>

This is where Jquery function called :-

<input type="checkbox" autocomplete="off" onchange="asfd(this.checked);"/>

This is the div which will hide or to show:-

<div id="renew" style="display: none ;">
........Content.........
</div>

You can achieve it with this

$('#date_range').hide();

$('[name=filter_radio]').on('click', function(){
    if($('#range').is(':checked')){
        $('#date_range').show();
    } else {
        $('#date_range').hide();
    }
});

Hope its simple and clear ..

You could try this

Markup :

<label><input id="last24" type="radio" name="filter_radio" value="1" />Last 24hrs</label>
<label><input id="WTD" type="radio" name="filter_radio" value="2" />WTD</label>
<label><input id="last_week" type="radio" name="filter_radio" value="3" />Last Week</label>
<label><input id="range" type="radio" name="filter_radio" value="4" />Range</label>

<div id="filter_radio-4" class="toHide" style="display:none">
    <br />
            <label for="from">From: </label><input type="text" id="from" name="from"/>
            <label for="to">To: </label><input type="text" id="to" name="to" />
</div>

Javascript :

$(function() {
    $("[name=filter_radio]").click(function(){
            $('.toHide').hide();
            $("#filter_radio-"+$(this).val()).show('slow');
    });
 });​

DEMO : http://jsfiddle.net/chridam/FSrBC/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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