保持按钮禁用,直到当前页面重新加载(使用 Jquery)

我有一个搜索栏index.php,当我输入 ID 时,会从 中获取 SQL 数据fetch.php。fetch.php 包含每行 SQL 数据的“AddToZip”按钮。当我单击选定的按钮时,选定的按钮将被禁用(直到这里为止都正常)。但是,当我清除搜索栏以写入不同的 ID 或相同的 ID 时,“AddToZip”的所有禁用按钮都会重新激活/启用。当用户多次使用搜索栏时,如何保持它们禁用,并且无论如何,它们仅在用户刷新页面时才启用。

<php code>


<?php

session_start();

...

...

...

?>


<html>

 <head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>LiveData</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

 </head>

 <body>

  <div class="container">

   <br />

   <h2 align="center">Live Data Search</h2><br />

   <div class="form-group">

    <div class="input-group">

     <span class="input-group-addon">Search</span>

     <input type="text" name="search_text" id="search_text" placeholder="Search by ID" class="form-control" />

    </div>

    

   </div>

   <p>

        <a href="logout.php" class="btn btn-danger">Sign Out of Your Account</a>

        

    </p>

    

   <br />

   <div id="result"></div>

  </div>

</html>




<script>

$(document).ready(function(){

    $('#search_text').keyup(function(){

        var txt = $(this).val();

        if(txt != '')

        {

            $.ajax({

                url:"fetch.php",

                method: "post",

                data: {search:txt},

                dataType: "text",

                success: function(data)

                {

                    $('#result').html(data);

                }

            });

        }

        else

            if(txt == '')

        {

            $.ajax({

                url:"null.php",

                method: "post",

                data: {search:txt},

                dataType: "text",

                success: function(data)

                {

                    $('#result').html(data);

                }

            });

        }

        

            

    });

});


子衿沉夜
浏览 86回答 2
2回答

蓝山帝景

您可以添加AddToZip.php的代码吗?您可以将所有id存储到会话值中,并检查fetch.php上的此数组中是否包含该 id 。AddToZip.php:session_start();if (!isset($_SESSION['added_to_zip_ids']))&nbsp; &nbsp; $_SESSION['added_to_zip_ids'] = [];if (array_search($_GET['id'], $_SESSION['added_to_zip_ids']) === false)&nbsp; &nbsp; $_SESSION['added_to_zip_ids'][] = $_GET['id'];获取.php:<?php...session_start();if (!isset($_SESSION['added_to_zip_ids']))&nbsp; &nbsp; $_SESSION['added_to_zip_ids'] = [];if(mysqli_num_rows($result) > 0){&nbsp;$output .= '<h4 align = "center">Search Result</h4>';&nbsp;$output .= '<div class="table-responsive">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <table class = "table table bordered">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>ID</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; while($row = mysqli_fetch_array($result))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $output .= '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'.$row["ID"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><button '.(array_search($row["ID"], $_SESSION['added_to_zip_ids']) === false ? '' : 'disabled').' type="button" class="btn btn-primary" onclick="addToZip(\''.$row["ID"].'\', this)"><i class="fa fa-pdf" aria-hidden="true"> </i>Add to Zip</button></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ';&nbsp; &nbsp; }&nbsp; &nbsp; echo $output;&nbsp; &nbsp;&nbsp;}...?>要清除存储的值,您有两种解决方案:保留其他会话数据会话开始();if (isset($_SESSION['added_to_zip_ids']))unset($_SESSION['added_to_zip_ids']);清除一切会话开始();session_unset();这些行可以位于服务器上的任何位置。

元芳怎么了

我建议使用事件处理程序“keyup”(就像您已经做的那样)和/或“更改”处理程序来激活功能(例如 check_disable_status)$('#search_text').keyup(check_disable_status) $('#search_text').change(check_disable_status)并且您还应该在 DOM 的“就绪”状态下启动此函数。在此函数中,您只需检查字段的当前“状态”,或者可能是业务逻辑的其他依赖项,并将字段始终设置为“启用”或“禁用”状态。
打开App,查看更多内容
随时随地看视频慕课网APP