Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果长度小于0,则在javascript中发出警报_Javascript_Jquery - Fatal编程技术网

如果长度小于0,则在javascript中发出警报

如果长度小于0,则在javascript中发出警报,javascript,jquery,Javascript,Jquery,嗨,我这里有两个文本字段的脚本 如果当前长度为0个字符。。。我想提醒大家,没有字符了 <html> <head> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> <title></title> </head> <body> <span id="limit">10&

嗨,我这里有两个文本字段的脚本

如果当前长度为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>

10

$(文档).ready(函数(){ //警报(“测试!!”); var组合文本长度=0; var限值=10; $(“input.text\u question\u 1”).live('keyup',函数(e){ 当前_长度=0; $.each($($(“input.text\u question\u 1”),函数(索引,值){ 当前长度+=value.value.length $(this.attr(“#limit”) }) $(“span#limit”).html(limit-当前长度) }) })
我试着把

if (limit < 0){
  alert("EXCEEDED!");
}
if(限制<0){
警报(“超过!”);
}

但不工作。

当前长度+=value.value.length似乎不正确。使用jQuery自己的
val()

if (current_length > limit){
  alert("EXCEEDED!");
}
由于您从不修改
limit
变量本身,因此它不能低于零

请注意,
警报
对用户并不特别友好。查找jQuery插件“lightBox”


此外,您似乎正在尝试使用
s,而
可能更合适

这似乎是错误的,但只是因为一个不吉利的变量命名。但它已经在工作了。我只需要声明一个警报,如果它超过了。检查这个小提琴,这是你想要的吗