jQuery计数字符达到限制

时间:2018-06-07 01:13:03

标签: javascript jquery

如何在到达时计数器停止的功能中添加限制(10个字符)?

输入10个字符后,$stmt = $PDO -> prepare("SELECT idP, a, b, c, d, e FROM tblP inner join tblC WHERE idPP = ? and aP = 0 and fP = 0 and idCP = idC"); $stmt -> bindParam(1, $_SESSION["idP"]); $stmt -> execute(); $query = $stmt -> fetchAll(PDO::FETCH_BOTH); $result = count($query); $query = array_chunk($query, $result); for ($i = 0; $i < $result; $i++){ $idP = $query[0][$i][0]; $dP = $query[0][$i][1]; $lP = $query[0][$i][2]; $nC = $query[0][$i][3]." ".$query[0][$i][4]; $cC = $query[0][$i][5]; echo '<div class="p2345"> <h3>P ID:</h3><br><a>'.$idP.'</a> <br><br> <h3>Description of P:</h3><br>'. $dP.' <br><br> <a href="'.$lP.'">Link of Archive</a> <br><br> <h3>N of the Cliente:</h3><br><a>'. $nC.' <br><br> <h3>C of the Cliente</h3><br><a>'. $cC.' <br><br> <input type="submit" name="btnC" value="Accept P"> <input type="submit" name="btnR" value="Refuse P"> </div> <br>'; echo "--------------------------------------------"; } 将停止计数(将显示10个字符)。如果被删除,它将倒数回到0。

chars-counter
$(document).ready(function() {
  var textarea = $("#my_textarea");
  textarea.keydown(function(event) {
    var numbOfchars = textarea.val();
    var len = numbOfchars.length;
    $(".chars-counter").text(len);
  });
});

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

$(document).ready(function() {
  var textarea = $("#my_textarea");
  textarea.keydown(function(event) {
    var numbOfchars = textarea.val();
    var len = numbOfchars.length;
    if (len == 10) {
        e.preventDefault(); // stop when it is 10
    } else {
        $(".chars-counter").text(len);
    }
  });
});

答案 1 :(得分:1)

如果您希望计数器在10处停止但字段继续工作,您可以像这样编辑代码:

+----+--------------+--------------+----------------+-----------------+-----------------------------------------+
| level_id|        uid1    |      uid2      |      uid3      |      uid4      |      status    |      level     |
+----+--------------+--------------+----------------------------------------------------------------------------+
|    1    | 576da2176b9867 | 57709be6486012 | 57709be6006055 | 57709be6486077 |     closed     |     level9     |
|    2    | 57709be6486012 | 577c132ed56645 | 57709be0000044 | 57709be6486033 |     closed     |     level5     |
|    3    | 577c132ed56645 | 577c1339f34e11 | 57709be6486002 | 57709be6486011 |     running    |     level3     |
|    4    | 577c1339f34e11 |  111111111111  |      NULL      |      NULL      |     opened     |     level2     |
+----+--------------+--------------+----------------------------------------------------------------------------+

答案 2 :(得分:1)

  1. 用于输入事件(on input event
  2. $(document).ready(function() {
      var textarea = $("#my_textarea");
      textarea.on('input',function(event) {
        var numbOfchars = $(this).val();
        var len = numbOfchars.length;
        var text;
        len <= 10 ?  text = len : text = "over"
        $(".chars-counter").text(text);
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea id="my_textarea" class="uk-textarea" rows="5" name="text"></textarea>
    <h1 class="chars-counter">0</h1>