简体   繁体   English

如果长度小于0,则JavaScript中会发出警报

[英]If length is less than 0 alert in javascript

Hi i have here a script for two text fields. 嗨,我这里有两个文本字段的脚本。

If the current length is 0 character... I want to alert that no characters left! 如果当前长度为0个字符,我想提醒您,没有字符了!

<html>
  <head>
   <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
     <title></title>
  </head>
  <body>
    <span id="limit">10</span><br/>
    <input type="text" class="text_question_1"><br>
    <input type="text" class="text_question_1">
  </body>
  <script type="text/javascript">
  $(document).ready(function(){
    //alert("test!!");
    var combined_text_length = 0;
    var limit = 10;
    $("input.text_question_1").live('keyup', function (e){
      current_length = 0;


      $.each($("input.text_question_1"), function(index, value){
        current_length += value.value.length
        $(this).attr("#limit")
      })
      $("span#limit").html(limit - current_length)
    })
  })
  </script>
</html>

I tried to put... 我试着放...

if (limit < 0){
  alert("EXCEEDED!");
}

But not working. 但是没有用。

current_length += value.value.length seems wrong. current_length += value.value.length似乎是错误的。 Use jQuery's own val() to retrieve the input field value as a string 使用jQuery自己的val()以字符串形式检索输入字段的值

Try 尝试

if (current_length > limit){
  alert("EXCEEDED!");
}

As you are never modifying the limit variable itself, it can not get lower than zero. 由于您永远不会修改limit变量本身,因此它不能小于零。

Note that an alert is not particularly user-friendly. 请注意, alert并非特别友好。 Lookup the jQuery plugin "lightBox". 查找jQuery插件“ lightBox”。

Also, it seems you are trying to use a <input> s where a <textarea> might be more appropriate. 另外,似乎您正在尝试使用<input> ,其中<textarea>可能更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM