jquery - check length of input field?

前端 未结 2 544
半阙折子戏
半阙折子戏 2020-12-23 16:25

The code below is intended to enable the submit button once the user clicks in the textarea field. It works, but I\'m trying to also make it so that it\'s only enabled if t

相关标签:
2条回答
  • 2020-12-23 16:45

    If you mean that you want to enable the submit after the user has typed at least one character, then you need to attach a key event that will check it for you.

    Something like:

    $("#fbss").keypress(function() {
        if($(this).val().length > 1) {
             // Enable submit button
        } else {
             // Disable submit button
        }
    });
    
    0 讨论(0)
  • 2020-12-23 17:01

    That doesn't work because, judging by the rest of the code, the initial value of the text input is "Default text" - which is more than one character, and so your if condition is always true.

    The simplest way to make it work, it seems to me, is to account for this case:

        var value = $(this).val();
        if ( value.length > 0 && value != "Default text" ) ...
    
    0 讨论(0)
提交回复
热议问题