douxiong0668 2013-07-24 22:38 采纳率: 100%
浏览 67
已采纳

清除div中的所有字段输入hide()jquery:带有php函数调用的单选按钮

I'm quite new to jQuery, so I'm having trouble with this.

I have divs that show or hide according to which radio button is selected, but I cannot figure out how to make all of the fields reset if a div is hidden.

The divs have date input fields with php drop down menus that are set to Null or "" in the first option. There are also some additional radio buttons within the main toggled divs (with more date choices/menus) - So a div within a div with more radio buttons...

Ultimately, I want to clear/reset everything within any hidden divs.

Here is my Jquery (I was getting desperate towards the end and started looking for straight up javascript after a while - don't know what I'm doing there either).

The .desc class is the main radio button choice that shows or hides one of two divs with various other choices.

    $(document).ready(function(){ 
    $("input[name='date']").change(function() {
        var test = $(this).val();
        $(".desc").hide();
        $("#"+test).show();
        $("input[type='hidden']").remove();
    }); 

    $("input[name='year']").change(function() {
        var test = $(this).val();
        $(".rnge").hide();
        $("#"+test).show();
        $("input[type='hidden']").remove();
    }); 
});

Here is the html (sorry for not adding it sooner.

<div id="formElementLeft">
    <h4>Date:</h4>
<p>Is this an exact or an approximate date?</p> 
    <p><input type="radio" name="date" value="exact"/>Exact</p>
    <p><input type="radio" name="date" value="circa"/>Approximate</p>   
</div>       
    <!--if exact show this-->
<div id="exact" class="desc" style="display:none;">
<p>Enter the exact date: <? echo date_picker(); ?><input type="radio" name="bce_ce"   value="BCE"/>BCE  <input type="radio" name="bce_ce" value="CE"/>CE</p>        
</div>
    <!--if approximate show this-->
<div id="circa" class="desc" style="display:none;">
    <p>Enter a date range.</p>             
    <p>is this a single year or a range of years?
    <p><input type="radio" name="year" value="single"/>Single year</p>
    <p><input type="radio" name="year" value="range"/>Range of years</p>        
    <div id="single" class="rnge" style="display:none;">
    <p><? echo year_picker(); ?></p>
    <p><? echo month_picker(); ?> (optional)</p></div>
    <div id="range" class="rnge" style="display:none;">
    <p>Between <? echo year_picker(); ?><input type="radio" name="bce_ce" value="BCE"/>BCE     <input type="radio" name="bce_ce" value="CE"/>CE;</p> 
    <p>and <? echo year_picker(); ?><input type="radio" name="bce_ce" value="BCE"/>BCE  <input type="radio" name="bce_ce" value="CE"/>CE</p></div>               
</div>

Thanks in advance for helping poor, confused me.

  • 写回答

2条回答 默认 最新

  • dtsc1684 2013-07-29 13:59
    关注

    Thank you for the tips. It took walking away from it for a day and looking at it with fresh eyes to really understand the syntax. Eventually my tinkering led to this, which actually works(quite proud of myself)!! I nested the .html method in the .hide method so the .html method would be dependent on the .hide method. The trouble with the .each method was that it prevented the divs from showing at all when a radio button was selected.

    $(document).ready(function(){ 
    $("input[name='date']").change(function() {
        var test = $(this).val();
        $(".desc").hide(function(){
            $(this).html($(this).html());
            });     
        $("#"+test).show();
    }); 
    
    $("input[name='year']").change(function() {
        var test = $(this).val();
        $(".rnge").hide(function(){
            $(this).html($(this).html());
            }); 
        $("#"+test).show();     
    }); 
    

    } });

    EDIT: Discovered a glitch. using the .desc class was not specific enough for one of the divs so it was clearing some things and not allowing one div to open. This is better and does exactly what I want. I had to nest the .html method in the first .show function for this and specify a div id instead of a class to make it work.

    $(document).ready(function(){ 
        $("input[name='date']").change(function() {
            var test = $(this).val();
            $(".desc").hide();      
            $("#"+test).show(function(){
                $("#exact").html($("#exact").html());
                });
        }); 
    
        $("input[name='year']").change(function() {
            var test = $(this).val();
            $(".rnge").hide(function(){
                $(this).html($(this).html());
                }); 
            $("#"+test).show();     
        }); 
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)