显示隐藏的divs刷新 [英] show hidden divs on refresh
问题描述
我很困难的时候想知道如何使我的代码正常工作。我目前有一个div从隐藏在加载和变化一旦选择从一个保管箱。
I'm having a very difficult time trying to figure out how to make my code function properly. I currently have a div that is hidden on load and changes once a selection is made from a dropbox.
一旦选择(美国)由保管箱进行,隐藏的div出现,用户将继续填写表单。
Once the selection (United States) is made from the dropbox, the hidden div appears, and the user and continue filling out the form.
当用户保存表单时,我的问题出现,然后回到该页面 - > div不再可见(我相信这是因为它的固有显示值设置为none;)。用户必须从下拉(美国)中再次选择适当的值才能查看div。
My issue arrises when the user saves the form, and then comes back to that page--> the div is no longer visible (I believe that is because its inherent display value is set to "none;"). The user must once again select the proper value from the drop down (United States) in order to view the div.
如果正确的值位于保管箱中,我想知道如何在页面刷新时使div显示。我在HAML中编程,所有的选项值(美国,国际)都位于constants.rb文件中。
I want to know how I can make the div visible on a page refresh if the correct value is in the dropbox. I am programming in HAML and all the option values (United States, International) are located in the constants.rb file.
我相信这可以通过cookie (如果可能,我想避免)或通过某种jscript / jquery验证页面加载...不幸的是,我完全不熟悉这些东西,所以我请求任何帮助。
I believe this is possible to achieve either via cookies (which I'd like to avoid if possible) or through some sort of jscript/jquery validation on page load... unfortunately I'm not at all familiar with either of those things, so I kindly ask for any help.
我真诚地感谢你花时间帮助我。谢谢!
I sincerely appreciate you taking the time to help me out. Thank you!
HAML代码:
= f.select :location, LOCATIONS, {}, :onChange => "showStates(this)", :prompt => true
#states_div{:style => "display:none;"}
%div{:class => "label_leftalign field"}
= f.label :state, "State"
= f.select :state, STATES, :prompt => true
Javascript:
Javascript:
function showStates obj)
function showStates(obj)
{
if(obj[obj.selectedIndex].value == 'United States')
{
document.getElementById('states_div').style.display = 'block';
}
else
{
document.getElementById('states_div').style.display = 'none';
}
}
推荐答案
你可以通过在页面加载上运行showStates(this)来实现这一点。只需将其添加到模板的底部:
You can achieve this by just running showStates(this) on page load. Just add this to the bottom of your template:
:javascript
window.onload = function() {
select_location = document.getElementById('<id of select tag generated for location>')
if(select_location[select_location.selectedIndex].value == 'United States')
{
document.getElementById('states_div').style.display = 'block';
}
else
{
document.getElementById('states_div').style.display = 'none';
}
};
然后,如果美国在其将显示的表单中存在。
Then if United States is present in the form it will display it.
这篇关于显示隐藏的divs刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!