简体   繁体   English

在JavaScript中加载页面后如何显示div?

[英]How to show a div after page loads in JavaScript?

Hi I have a Laravel Blade template where I built a search function and I want to load a div after the page has reloaded, when I submit the form. 嗨,我有一个Laravel Blade模板,我在其中构建了搜索功能,我想在提交表单时在页面重新加载后加载div。 Otherwise when the page loads with the results, the results don't show. 否则,当页面加载结果时,结果将不会显示。 I want the Javascript to execute while on the page (hide the div), and then when the page refreshes then the div must become visible. 我希望Javascript在页面上运行(隐藏div),然后在页面刷新时div必须变得可见。

My HTML form is: 我的HTML表单是:

<form action="" class="search-form" method="GET" onsubmit="showDiv();">
    <input type="text" name="location" placeholder="Search" required>
    <button class="search-btn" type="submit"><i class="flaticon-026-search"></i></button>
  </form>

And the div I want to show after page load is: 我想在页面加载后显示的div是:

<div id="hideDiv" style="display:none">
  @foreach($locations as $location)
    @foreach($location->landmark as $landmark)
       <p>{{$landmark->name}}</p>
    @endforeach
  @endforeach
</div>

My JavaScript: 我的JavaScript:

function showDiv() {
   var div = document.getElementById("hideDiv");
   if (div.style.display == 'none') {
     div.style.display = '';
   }
   else {
     div.style.display = 'block';
   }
 }

Write window.onload = showDiv; 写window.onload = showDiv; before your function 在您工作之前

If you have only one submit button on same page than you can use like bellow (not tested, just try to suggest) 如果同一页面上只有一个提交按钮,您可以像下面这样使用(未经测试,请尝试建议)

@if (Request::isMethod('post'))
 <div id="hideDiv" style="display:none">
   @foreach($locations as $location)
     @foreach($location->landmark as $landmark)
       <p>{{$landmark->name}}</p>
    @endforeach
   @endforeach
 </div>
@endif

PS: try to identify ispostback event in Page or Javascript which one is preferable. PS:尝试识别Page或Javascript中的ispostback事件,哪一个是可取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM