问题
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