刷新时显示隐藏的 div
我很难弄清楚如何使我的代码正常运行。我目前有一个 div,它在加载时隐藏,并在从保管箱中进行选择后发生变化。
从保管箱中选择(美国)后,隐藏的 div 就会出现,用户可以继续填写表单。
当用户保存表单,然后返回到该页面时,我的问题出现了--> div 不再可见(我相信这是因为其固有的显示值设置为“none;”)。用户必须再次从下拉列表中选择正确的值(美国)才能查看 div。
我想知道如果保管箱中有正确的值,如何使 div 在页面刷新时可见。我正在使用 HAML 进行编程,所有选项值(美国、国际)都位于constants.rb 文件中。
我相信这可以通过cookie(如果可能的话我想避免)或通过页面加载时的某种jscript/jquery验证来实现......不幸的是我对这些事情都不熟悉,所以我恳请任何帮助。
我真诚地感谢您抽出时间来帮助我。谢谢你!
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:
函数 showStates(obj)
{
if(obj[obj.selectedIndex].value == 'United States')
{
document.getElementById('states_div').style.display = 'block';
}
else
{
document.getElementById('states_div').style.display = 'none';
}
}
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.
Once the selection (United States) is made from the dropbox, the hidden div appears, and the user and continue filling out the form.
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.
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.
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 code:
= 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:
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';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需在页面加载时运行 showStates(this) 即可实现此目的。只需将其添加到模板的底部:
然后,如果表单中存在美国,它将显示它。
You can achieve this by just running showStates(this) on page load. Just add this to the bottom of your template:
Then if United States is present in the form it will display it.