javascript (jQuery) - 单个脚本在我的 2 个单独页面中不起作用

标签 javascript php jquery

我有两个单独的页面:issuance_filter.phpmemo_list.php 以及 1 个脚本。我的问题是,我无法使其在 issuance_filter.php 上工作,但在 memo_list.php 上工作得很好。但有时反之亦然。

这是我的代码:

issues_filter.php

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
<script type="text/javascript" class="init">

$(document).ready(function() {
    $('#example').DataTable( {
        "order": [[ 0, "desc" ]]
    } );
} );

</script>

<h2>Issuances</h2>

<table id="example" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th width="8%">ID</th>
            <th width="18%">Issuance Type</th>
            <th width="18%">Category</th>
            <th width="18%">In Charge</th>
            <th width="18%">Date Released</th>
            <th align="18%">Issuance Title</th>
        </tr>
    </thead>
    <tbody>

        <?php

            include "../dbconnect/dbconnection2015.php";
            mysql_query("SET NAMES utf8");          
            /////////////////////////////////

            /*Values from the other page*/
                $type = htmlentities($_POST['type']);
                $year = htmlentities($_POST['year']);
                $in_charge = htmlentities($_POST['in_charge']);
                $category = htmlentities($_POST['category']);
                mysql_query("SET NAMES utf8");

                if (empty($type) AND !empty($year) AND !empty($category) AND !empty($in_charge)) { 
                    $filter = "WHERE Year = '$year' AND Category = '$category' AND In_Charge = '$in_charge'";
                }
                elseif(empty($year) AND !empty($type) AND !empty($in_charge) AND !empty($category)){
                    $filter = "WHERE Issuance_Type = '$type' AND Category = '$category' AND In_Charge = '$in_charge'";
                }
                elseif(empty($in_charge) AND !empty($year)  AND !empty($type)  AND !empty($category)){
                    $filter = "WHERE Year = '$year' AND Category = '$category' AND Issuance_Type = '$type'";
                }
                elseif(empty($category) AND !empty($year) AND !empty($type) AND !empty($in_charge)){
                    $filter = "WHERE Year = '$year' AND Issuance_Type = '$type' AND In_Charge = '$in_charge'";
                }
                elseif(empty($type) AND empty($year) AND !empty($in_charge)  AND !empty($category)){
                    $filter = "WHERE Category = '$category' AND In_Charge = '$in_charge'";
                }
                elseif(empty($in_charge) AND empty($category) AND !empty($year) AND !empty($type)){
                    $filter = "WHERE Year = '$year' AND Issuance_Type = '$type'";
                }
                elseif(empty($type) AND empty($in_charge) AND !empty($year) AND !empty($category)){
                    $filter = "WHERE Category = '$category' AND Year = '$year'";
                }
                elseif(empty($category) AND empty($year) AND !empty($in_charge) AND !empty($type)){
                    $filter = "In_Charge = '$in_charge' AND Issuance_Type = '$type'";
                }
                elseif(empty($type) AND empty($category) AND !empty($in_charge) AND !empty($year)){
                    $filter = "WHERE In_Charge = '$in_charge' AND Year = '$year' ";
                }
                elseif(empty($in_charge) AND empty($year) AND !empty($type) AND !empty($category)){
                    $filter = "WHERE Category = '$category' AND Issuance_Type = '$type'";
                }
                elseif(!empty($type) AND empty($year) AND empty($category) AND empty($in_charge)){
                    $filter = "WHERE Issuance_Type = '$type'";
                }
                elseif(!empty($year) AND empty($type) AND empty($category) AND empty($in_charge)){
                    $filter = "WHERE Year = '$year'";
                }
                elseif(!empty($category) AND empty($year) AND empty($type) AND empty($in_charge)){
                    $filter = "WHERE Category = '$category'";
                }
                elseif(!empty($in_charge) AND empty($year) AND empty($category) AND empty($year)){
                    $filter = "WHERE In_Charge = '$in_charge'";
                }
                elseif(!empty($type) AND !empty($in_charge) AND !empty($year) AND !empty($category)) {
                    $filter = "WHERE Issuance_Type = '$type' AND In_Charge = '$in_charge' AND Year = '$year' AND Category = '$category'";
                }
                else{
                    $filter = "";
                }
                // echo $filter;

                $sql = "SELECT * FROM issuances $filter ORDER BY Issuance_ID DESC";
                $fetch_num_memo = mysql_query($sql);
                if ($fetch_num_memo && mysql_num_rows($fetch_num_memo) > 0){
                        while($row = mysql_fetch_assoc($fetch_num_memo)){
                        $string = $row['Issuance_Heading'];                     
                        echo "<tr>";
                        echo "<td>".$row['Issuance_ID']."</td>";    
                        echo "<td>".$row['Issuance_Type']."</td>";
                        echo "<td>".$row['Category']."</td>";
                        echo "<td>".$row['In_Charge']."</td>";
                        echo "<td>".$row['Date_Released']."</td>";
                        echo "<td><a class='filename' href='{$row['URL']}' target='_blank'>{$row['Issuance_Title']}</a></td></tr>";
                    }
                }
                else {
                    # Do Nothing
                }
        ?>
    </tbody>                        
</table>

memo_list.php

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />
<script type="text/javascript" class="init">

$(document).ready(function() {
    $('#example').DataTable( {
        "order": [[ 0, "desc" ]]
    } );
} );

</script>

<h2>Issuances</h2>

<table id="example" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th width="5%">ID</th>
            <th width="15%">Issuance Type</th>
            <th width="15%">Date Released</th>
            <th align="15%">Issuance Title</th>
        </tr>
    </thead>
    <tbody>

        <?php

            include "dbconnect/dbconnection2015.php";
            mysql_query("SET NAMES utf8");
            $fetch_num_memo = mysql_query("SELECT * FROM issuances WHERE $filter ORDER BY Issuance_ID DESC");
            if ($fetch_num_memo && mysql_num_rows($fetch_num_memo) > 0){
                while($row = mysql_fetch_assoc($fetch_num_memo)){
                $string = $row['Issuance_Heading'];                     
                echo "<tr>";
                echo "<td>{$row['Issuance_ID']}</td>";  
                echo "<td> {$row['Issuance_Type']}</td>";
                echo "<td>".$row['Month'].". ".$row['Date'].", ".$row['Year']."</td>";
                echo "<td><a class='filename' href='{$row['URL']}' target='_blank'>{$row['Issuance_Title']}</a></td></tr>";
                }
            }
            else {
            echo '<script type="text/javascript">
            window.location = "sdo_issuances.php"
            </script>';
            }
        ?>

    <tbody>                        
</table>

和脚本:

<script type='text/javascript'>
$(document).ready(function(){
    $('.filename').click(function(){
        var filename = ($(this).text());
         $.post('sdo_insert.php', {postname:filename}, function (data) {
                // $('.insert').html(data);
         });
         alert('You clicked : ' + filename);
       });
    });
</script>

我无法使脚本在两个页面中都工作,也无法找出问题所在。请帮我解决这个问题。谢谢

最佳答案

试试这个

  <script type='text/javascript'>
    $(function() {
    $('.filename').click(function(){
     var filename = ($(this).text());
     $.post('sdo_insert.php', {postname:filename}, function (data) {
            // $('.insert').html(data);
     });
     alert('You clicked : ' + filename);
   });
  });
</script>

关于javascript (jQuery) - 单个脚本在我的 2 个单独页面中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44147184/

相关文章:

php - 如何让两个不同的 PDF 文件出现在同一个 iframe 中?

php - Composer 删除了每个部署的依赖关系。

jquery - 检查链接目标与 url

javascript - jQuery 只允许选定的字符按键功能

javascript - 从 HTML DOM 中选择不同的标签集

javascript - 标记 + 标签旋转 - Google Maps API v3

javascript - 简单地访问其他命名空间的成员

javascript - block 作用域函数 ECMAScript 6 奇怪的行为

javascript - 在网站上使用多个 'click'

php - 我可以使用 php 脚本覆盖/取消 HTTP 响应 header 字段,以便不设置该字段吗?