maintain the state of a radio-toggled div after page reload

风格不统一 提交于 2019-12-12 02:53:34

问题


In my form there are some radio buttons - when the radio is selected - the div appears, when another one is selected - the div hides. After submitting a form if there are some errors, page reloads and the div should still show (if the radio was selected). The solution below works only for checkboxes - how to make it work for radio's?
https://stackoverflow.com/a/8406409/1137417
Here is my code:

    $(function(){

    $('#Q07_03, #Q07_07, #Q08_03').hide();

    // checkboxes
    $('#Q07_03').toggle($('#Q07_02').is(':checked'));
    $('#Q07_02').click(function() {
    $('#Q07_03').toggle(); });

    $('#Q07_07').toggle($('#Q07_06').is(':checked'));
    $('#Q07_06').click(function() {
    $('#Q07_07').toggle(); });

    // here code for radio ?
    // #Q08_03 is the div to be displayed;
    // #Q08_02 is the one that should be selected
    })

回答1:


Why don't you use ajax to submit the form? You will not face these kind of issues. Well to answer your question you can try this.

//I don't know how many radio buttons are there on the page and what there names are
//you can accordingly change the radio button selector to check if it is checked or not

$(document).ready(function() {
     if($("input:radio:first").is(":checked"))
         $('divToShowHide').show()
     else
         $('divToShowHide').hide()
});


来源:https://stackoverflow.com/questions/9004958/maintain-the-state-of-a-radio-toggled-div-after-page-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!