简体   繁体   English

jQuery,用于将检查值保留在单选按钮中

[英]Jquery for retaining the checked value in radio button

I am having two radiobuttons, 我有两个单选按钮,

<div class="class1">
<span><input name="chk1" type="radio" checked="checked" id="check1"/>Option 1</span>
<span><input name="chk2" type="radio" id="check2"/>Option 2</span>
</div>

Now, i am displaying a hidden div using Jquery when the radio button with an ID check1 is cliked and hiding the same div when the radio button with an ID check2 is clicked, here is the code for that, 现在,当单击ID为check1的单选按钮时,我将使用Jquery显示一个隐藏的div,单击ID为check2的单选按钮时,将隐藏相同的div,这是该代码,

$(document).ready(function(){
    $("#check1").click(function(){
                                            $("#detail").slideDown("slow");
    });


    $("#check2").click(function(){ 
                                            $("#detail").slideUp("slow");
    })
}); 

My problem is, when an ID check1 is clicked(Div is displayed now) and the values are posted to the server and while return back to the same page, the div is not getting displayed, even though the option check1 is clicked. 我的问题是,当单击ID check1(现在显示Div)并将值发布到服务器,并且返回到同一页面时,即使单击了选项check1,div也不会显示。 Is there any other way to retain the div using attr :checked or something? 还有其他方法可以使用attr:checked保留div吗? Appreciate your help... 感谢您的帮助...

Check online working demo http://jsfiddle.net/kbsYS/ .. i guess this is what you actually want 检查在线工作演示http://jsfiddle.net/kbsYS/ ..我想这就是您真正想要的

Your jquery code is right .. problem is in your HTML you should use same name in radio box 您的jquery代码正确..问题出在HTML您应该在单选框中使用相同的名称

HTML 的HTML

<div class="class1">
<span><input name="chk1" type="radio" checked="checked" id="check1"/>Option 1</span>
<span><input name="chk1" type="radio" id="check2"/>Option 2</span>
</div>
$(document).ready(function(){
    if ($("#check1:checked").length) {
        $("#detail").slideDown("slow");
    }

    $("#check1").click(function(){
        $("#detail").slideDown("slow");
    });


    $("#check2").click(function(){ 
        $("#detail").slideUp("slow");
    })
}); 

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

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