簡體   English   中英

頁面重新加載時Javascript無法正常工作

[英]Javascript is not working when page reloads

這是我的代碼

<!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>

基本上,search.php文件包含用於顯示結果的mysql查詢。

問題是,當重新加載此頁面時(因為我將POST變量發送到此頁面),javascript無法正常工作。 結果,我無法在結果頁面上顯示或隱藏某些div(因為結果頁面的格式與此頁面相同)。

難道我做錯了什么?? 請幫忙。 謝謝。

在較大的JS部分中,只有函數定義 ,但是現在調用 -為了使它們無論如何都能運行,您需要調用它們,例如:

noSearch();
advancedSearch();

並且這部分似乎與$_POST檢查沒有關系..為避免不在search.php中調用它們,您應該從主文件中刪除if(isset($_POST["cari"])){它在search.php中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM