在页面重新加载后保持无线电切换div的状态 [英] maintain the state of a radio-toggled div after page reload
问题描述
在我的表单中有一些单选按钮 - 选择收音机时 - div出现,另一个选中 - div隐藏。提交表单后,如果有一些错误,页面重新加载和div应该仍然显示(如果广播被选中)。以下解决方案仅适用于复选框 - 如何使其适用于收音机?
https://stackoverflow.com/a/ 8406409/1137417
这里是我的代码:
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
})
推荐答案
为什么不使用ajax提交表单?你将不会面对这些问题。那么回答你的问题,你可以试试这个。
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()
});
这篇关于在页面重新加载后保持无线电切换div的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!