简体   繁体   English

页面重新加载时Javascript无法正常工作

[英]Javascript is not working when page reloads

this is my code 这是我的代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title> Daftar Berita </title>
    <meta http-equiv="content-type"
          content="text/html; charset=utf-8"/>
</head>
<body>

    <form method="post" action="">
        <?php include 'formpencarian.html.php'; ?>

    <div id="newssection" style="margin-top: 50px; clear: both; display: block;">
        <?php include 'newssection.php'; ?>
    </div>

    <?php
            if(isset($_POST["cari"])){
                echo "<div id='searchsection' style='margin-top: 50px; clear: both; display: block;'>";
                include 'search.php';
                echo "</div>";
                echo "<script>document.getElementById('newssection').style.display = 'none';</script>";
            }
    ?>

    <script>
        function showMore(id){
            document.getElementById('isi'+id).style.display = "block";
            document.getElementById('more'+id).style.display = "none";
            document.getElementById('less'+id).style.display = "block";
        }

        function showLess(id){
            document.getElementById('isi'+id).style.display = "none";
            document.getElementById('more'+id).style.display = "block";
            document.getElementById('less'+id).style.display = "none";
        }

        function advancedSearch(){
            document.getElementById('advanced').style.display = "none";
            document.getElementById('searching').style.display = "block";
            document.getElementById('no').style.display = "block";
        }

        function noSearch(){
            document.getElementById('advanced').style.display = "block";
            document.getElementById('searching').style.display = "none";
            document.getElementById('no').style.display = "none";
        }

    </script>
</body>
</html>

basically, the search.php file contains mysql query for displaying results. 基本上,search.php文件包含用于显示结果的mysql查询。

the problem is, when this page reloaded (because i sent the POST variable to this page) the javascript doesn't work. 问题是,当重新加载此页面时(因为我将POST变量发送到此页面),javascript无法正常工作。 as a result, i can't show or hide certain divs on my results page (because the results page is formatted just the same as this page). 结果,我无法在结果页面上显示或隐藏某些div(因为结果页面的格式与此页面相同)。

Am i doing something wrong?? 难道我做错了什么?? Please help. 请帮忙。 Thanks. 谢谢。

in the bigger JS part there are only function definitions , but now calls - to make them run in any case, you need to call them, like: 在较大的JS部分中,只有函数定义 ,但是现在调用 -为了使它们无论如何都能运行,您需要调用它们,例如:

noSearch();
advancedSearch();

and this part doesn't seem to be related to $_POST checking .. to avoid not calling them in search.php, you should remove if(isset($_POST["cari"])){ check from your main file and do it inside of the search.php 并且这部分似乎与$_POST检查没有关系..为避免不在search.php中调用它们,您应该从主文件中删除if(isset($_POST["cari"])){它在search.php中

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

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