开发者

How to ensure a disable button when number of characters exceed 160

i have a script that tells my button to go disable mode when number of characters go beyond 160 but it is not working. any idea why?

this is my javascript:

     <script>
   function CountLeft(field,max){
       //disable post
    开发者_如何学编程   if (field.value.length > max)
          $('#workroom_submit').attr("disable","disable");
       // enable post 
       if (field.value.length <= max)
          $('#workroom_submit').attr("disable","");
       }
    </script>

this is my html:

  <textarea cols="50" rows="3" name="postMsg"  onKeyDown="CountLeft(this.form.text,160);" onKeyUp="CountLeft(this.form.text,160);"></textarea>

  <input type="submit" name="workroom_submit" id="workroom_submit" disable="" value="Post" onclick="updateMsg()"/>


I made a fiddle with an example : http://jsfiddle.net/vKFws/

Removed all the in HTML event binding and used jQuery's event binding

<textarea cols="50" rows="3" name='postMsg' id='postMsg'></textarea>

<input type="submit" id="workroom_submit" value="Post"/>

$('#postMsg').keyup(function(){
    var thetext = $(this).val();

    if (thetext.length > 60) {
        $('#workroom_submit').attr('disabled', 'disabled');
    } else {
        $('#workroom_submit').removeAttr('disabled');
    }
})


jQuery id smart to handle all input and return you value by calling val() method.

function CountLeft(field,max){
       $field = $(field) // convert the field in jQuery object
       //disable post
       if ($field.val().length > max)
          $('#workroom_submit').attr("disabled","disabled"); // the attr name is disabled
       // enable post 
       if ($field.val().length <= max)
          $('#workroom_submit').removeAttr("disable");
       }


correct attribute is disabled http://www.w3schools.com/TAGS/att_input_disabled.asp


You can get an idea from the below link :

  • Limiting the no of characters inside a textarea

Try with disabled attribute.

disabled="disabled"; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜