使用jQuery提交表单后如何关注最后输入的编辑

时间:2014-11-27 16:53:35

标签: jquery html cookies jquery-cookie

我创建了一个网站,其中有一个包含超过13,000条记录的表格,我正在尝试将每个字母用户输入网站刷新的过滤器用这个字母过滤记录,但该字段会失去对提交的关注。

我该如何解决这个问题?

我尝试使用Cookie跟随sephiith问题的示例,但它仍然失败。 我如何提交示例:

$(function() {
  $('input').keyup(function() {
    this.value = this.value.toLocaleUpperCase();
    this.form.submit();
  });
});

页面代码

<?php
session_start();
if (!isset($_SESSION['LOGIN'])) {
  header("location: index.php");
}

include("config.php");
require("rep.php");

if (isset($_SESSION['active']) && (time() - $_SESSION['active'] > 1800)) {
  session_unset();
  session_destroy();
}
$_SESSION['active'] = time();
?>
<html>
  <head>
    <? include("meta.txt"); ?>
    <title><? echo $config[TITLE]; ?></title>
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
    <style type="text/css">
      <!--
        @import url("<? echo $config[CSSFORM]; ?>");
        @import url("<? echo $config[CSS]; ?>");
      -->
    </STYLE>
    <script language="javascript" type="text/javascript" src="<? echo $config[JAVASCRIPT]; ?>"></script>
    <script language="javascript" type="text/javascript" src="./js/jquery.js"></script>
    <script language="javascript" type="text/javascript">
      $(function() {
        $("input").keyup(function(e) {
          var code = (e.keyCode ? e.keyCode : e.which);
          this.value = this.value.toLocaleUpperCase();
          if ( code == 13 ) {
            this.form.submit();
          }
        });
      });
    </script>
  </head>
  <body onload="showGMTDateTime()">
    <center>
      <!-- begin -->
      <div id="begin">
        <!-- Banner -->
        <? include("includes/banner.php"); ?>
        <!-- Menu -->
        <? include("includes/menu/menu01.html"); ?>
        <!-- Msg -->
        <? include("includes/msg.php"); ?>
        <div class="DivCad">
          <h1 class="prod" align="center">List of Produts</h1>
          <?php
            $rep      = new Rep();
            $GProd = $rep->SelectProd(0, 0, 0, $sql);
            if (!$GProd) {
              echo "<h4>List of Produts empty!</h4>";
            } else {
              $sql = "";
              $get = "";
              If ( $_REQUEST['group'] ) {
                $sql.= "   AND (GROUP LIKE '%".$_REQUEST['group']."%')";
                $get.= "&group=" . $_REQUEST['group'];
              }
              If ( $_REQUEST['model'] ) {
                $sql.= "   AND (MODEL LIKE '%".$_REQUEST['model']."%')";
                $get.= "&model=" . $_REQUEST['model'];
              }
              If ( $_REQUEST['line'] ) {
                $sql.= "   AND (LINE LIKE '%".$_REQUEST['line']."%')";
                $get.= "&line=" . $_REQUEST['line'];
              }
              If ( $_REQUEST['Prod'] ) {
                $sql.= "   AND (PROD LIKE '%".$_REQUEST['Prod']."%')";
                $get.= "&Prod=" . $_REQUEST['Prod'];
              }
              If ( $_REQUEST['desc'] ) {
                $sql.= "   AND (DESC LIKE '%".$_REQUEST['desc']."%')";
                $get.= "&desc=" . $_REQUEST['desc'];
              }
          ?>
          <table id="Prod">
            <form id="frmprodut" name="frmprodut" method="post" action="Prod.php">
              <thead>
                <tr>
                  <th width="30" align="center">
                    <input type="text" name="group" value="<?php echo $_REQUEST['group']; ?>" class="inputs" size="2" maxlength="4" placeholder="Group">
                  </th>
                  <th width="30" align="center">
                    <input type="text" name="model" value="<?php echo $_REQUEST['model']; ?>" class="inputs" size="9" maxlength="9" placeholder="Model">
                  </th>
                  <th width="30" align="center">
                    <input type="text" name="line" value="<?php echo $_REQUEST['line']; ?>" class="inputs" size="9" maxlength="9" placeholder="Line">
                  </th>
                  <th width="200" align="center">
                    <input type="text" name="Prod" value="<?php echo $_REQUEST['Prod']; ?>" class="inputs" size="15" maxlength="15" placeholder="Prod">
                  </th>
                  <th width="420" align="left">
                    <input type="text" name="desc" value="<?php echo $_REQUEST['desc']; ?>" class="inputs" size="20" maxlength="30" placeholder="Desc">
                  </th>
                  <th width="10" align="center">
                    <img alt="Add Filter" src="./imagens/filter_add.png" width="16" height="16" onclick="$('#frmprodut').submit()">
                  </th>
                  <th width="10" align="center">
                    <img alt="Clear Filter" src="./imagens/filter_del.png" width="16" height="16" onclick="javascript:window.location='Prod.php'; $('#frmprodut')[0].reset();">
                  </th>
                </tr>
              </thead>
            </form>
            <thead>
              <tr>
                <th align="center">Group</th>
                <th align="center">Model</th>
                <th align="center">Line</th>
                <th align="center">Prod</th>
                <th colspan="3" align="left">Desc</th>
              </tr>
            </thead>
            <tbody>
            <?php
              $GProd = $rep->SelectProd(0, 0, 0, $sql);

              $page     = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
              $numLine   = 1;
              $GTotal     = count($GProd);
              $reg  = 20;
              $next       = $page + 1;
              $prev        = $page - 1;
              $numpages = ceil($GTotal / $reg);
              $last  = $numpages - 1;
              $adjacent = 2;
              $begin     = ($reg * $page) - $reg;

              $AProd = $rep->SelectProd(1, $inicio, $reg, $sql);
              $TNow= count($AProd);

              for ($i = 0; $i < $TNow; $i++) {
                $Prod = new Prod($AProd[$i]['GROUP'], $AProd[$i]['MODEL'], $AProd[$i]['LINE'], $AProd[$i]['PROD'], $AProd[$i]['DESC']);
                if (($numLine % 2) == 0) {
                  echo "<tr>";
                } else {
                  echo "<tr class=\"alt\">";
                }
                echo "<td align=\"center\">" . $Prod->GetGroup() . "</td>";
                echo "<td align=\"center\">" . $Prod->GetModel() . "</td>";
                echo "<td align=\"center\">" . $Prod->GetLine() . "</td>";
                echo "<td align=\"left\">" . $Prod->GetProd() . "</td>";
                echo "<td align=\"left\" colspan=\"3\">" . $Prod->GetDesc() . "</td>";
                echo "</tr>";
                $numLine++;
              }
            ?>
            </tbody>
            <tfoot>
              <tr>
                <td colspan="7">
                  <div id="paging">
                    <ul>
                    <?php
                      if ($page > 1) {
                        $pagination = "<li><a href=\"Prod.php?page=" . $prev . $get ."\">Prev</a></li>";
                      }

                      if ($numpages <= 5) {
                        for ($i = 1; $i < $numpages + 1; $i++) {
                          if ($i == $page) {
                            $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\" class=\"active\">". $i ."</a></li>";
                          } else {
                            $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\">". $i ."</a></li>";
                          }
                        }
                      }

                      if ($numpages > 5) {
                        if ($page < 1 + (2 * $adjacentes)) {
                          for ($i = 1; $i < 2 + (2 * $adjacentes); $i++) {
                            if ($i == $page) {
                              $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\" class=\"active\">". $i ."</a></li>";
                            } else {
                                $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\">". $i ."</a></li>";
                            }
                          }
                          $pagination .= "<li class=\"dot\">...</li>";
                          $pagination .= "<li><a href=\"Prod.php?page=" . $last . $get . "\">".$last."</a></li>";
                          $pagination .= "<li><a href=\"Prod.php?page=" . $numpages . $get . "\">".$numpages."</a></li>";
                        } elseif ($page > (2 * $adjacentes) && $page < $numpages - 3) {
                          $pagination .= "<li><a href=\"Prod.php?page=1\">1</a></li>";
                          $pagination .= "<li><a href=\"Prod.php?page=2\">2</a></li>";
                          $pagination .= "<li class=\"dot\">...</li>";
                          for ($i = $page - $adjacentes; $i <= $page + $adjacentes; $i++) {
                            if ($i == $page) {
                              $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\" class=\"active\">". $i ."</a></li>";
                            } else {
                              $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\">". $i ."</a></li>";
                            }
                          }
                          $pagination .= "<li class=\"dot\">...</li>";
                          $pagination .= "<li><a href=\"Prod.php?page=" . $last . $get . "\">".$last."</a></li>";
                          $pagination .= "<li><a href=\"Prod.php?page=" . $numpages . $get . "\">".$numpages."</a></li>";
                        } else {
                          $pagination .= "<li><a href=\"Prod.php?page=1\">1</a></li>";
                          $pagination .= "<li><a href=\"Prod.php?page=2\">2</a></li>";
                          $pagination .= "<li class=\"dot\">...</li>";
                          for ($i = $numpages - (4 + (2 * adjacentes)); $i <= $numpages; $i++) {
                            if ($i == $page) {
                              $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\" class=\"active\">". $i ."</a></li>";
                            } else {
                              $pagination .= "<li><a href=\"Prod.php?page=" . $i . $get . "\">". $i ."</a></li>";
                            }
                          }
                        }
                      }

                      if ($next <= $numpages && $numpages > 2) {
                        $pagination .= "<li><a href=\"Prod.php?page=" . $next . $get . "\">Next</a></li>";
                      }
                      echo $pagination;
                    ?>
                    </ul>
                  </div>
                </td>
              </tr>
            </tfoot>
          </table>
          <?php } ?>
        </div>
      </div>
    </center>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

尝试用ajax来完成它所以页面根本不会刷新,在信件印刷机上刷新整个页面似乎不切实际:

HTML:

<input type="text" id="field"/>
<div id="showContent"></div>

jQuery的:

function findWithLetter(){
    $("#field").on("keyup",function(e){
        var Val = $(this).val();
        $.post("url\to\php",{Val:Val},function(answer){
            $("#showContent").html(answer);
        });      
    });
}

PHP:

$val = $_POST['Val'];
$ans = "";

$query = $mysqli->query("SELECT * FROM `table` WHERE name LIKE '$val%'");
while($row = $query->fetch_object()){
    $ans.="<div>$row->name</div>";
}
echo $ans;

答案 1 :(得分:0)

正如你所说,网站刷新所以我认为将该领域重点放在dom上。

$(function() {
  if($('input').val().length > 0){
    $('input').focus();
    }
});