2013-07-17 18 views
18

ユーザーがテキストを入力していない場合、送信ボタンを無効にしようとしています。入力フィールドが空の場合は、送信ボタンを無効にしてください。

一見すると、すべてうまくいくように見えますが、ユーザーがテキストを入力してから削除すると、送信ボタンが有効になります。ここで

は私のコードです:

$(document).ready(function(){ 
    $('.sendButton').attr('disabled',true); 
    $('#message').keyup(function(){ 
     if($(this).val.length !=0){ 
      $('.sendButton').attr('disabled', false); 
     } 
    }) 
}); 
+1

長さが0 – tymeJV

+1

あなたはそれを無効にすることはありません無効にelseステートメントを追加しますか?有効と無効の属性を使用するのは安全ではありません....あなたのブラウザでf12をクリックし、htmlの送信ボタンを見つけて、無効にしてください!入力が空であってもフォームを送信します。 – Doorknob

+0

どう攻撃についてであれば、それは – Elnaz

答えて

49

あなただけdocument.readyに無効にされているとDOMの準備ができているが、テキストボックスが空になったとき、あなたはあまりにからkeyupイベントにdisableに必要がある場合、これは一度だけ発生します。また$(this).val().length

$(document).ready(function(){ 
    $('.sendButton').attr('disabled',true); 
    $('#message').keyup(function(){ 
     if($(this).val().length !=0) 
      $('.sendButton').attr('disabled', false);    
     else 
      $('.sendButton').attr('disabled',true); 
    }) 
}); 

$(this).val.lengthを変更したり、あなたの代わりに、if文の条件演算子を使用することができます。属性がrecommended by jQuery 1.6ではなく、上記の障害者としても、代わりのattrの小道具を使用するなどのjQuery 1.6のよう

は、.ATTR()メソッドが設定されていない属性 ためundefinedを返します確認。 、取得してフォーム要素の確認、選択、または無効状態は、 .prop()メソッドを使用し、そのような としてDOMのプロパティを変更するにはjQuery docs

$(document).ready(function(){ 
    $('.sendButton').prop('disabled',true); 
    $('#message').keyup(function(){ 
     $('.sendButton').prop('disabled', this.value == "" ? true : false);  
    }) 
}); 
+2

ありがとう@Zenith、良い観察 – Adil

+0

ありがとう、@Adil。 –

+0

これはうまくいった –

6

このコード

$(document).ready(function(){ 
    $('.sendButton').attr('disabled',true); 

    $('#message').keyup(function(){ 
     if($(this).val().length !=0){ 
      $('.sendButton').attr('disabled', false); 
     } 
     else 
     { 
      $('.sendButton').attr('disabled', true);   
     } 
    }) 
}); 

チェックdemo Fiddle

をお試しください

if文のelse部分が欠落しています(textboxがempの場合は、もう一度ボタンを無効にします)。 TY)と括弧は()if($(this).val.length !=0){

2

で機能val後に行うには、この

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Jquery</title> 
    <meta charset="utf-8">  
    <script src='http://code.jquery.com/jquery-1.7.1.min.js'></script> 
    </head> 

    <body> 

    <input type="text" id="message" value="" /> 
    <input type="button" id="sendButton" value="Send"> 

    <script> 
    $(document).ready(function(){ 

     var checkField; 

     //checking the length of the value of message and assigning to a variable(checkField) on load 
     checkField = $("input#message").val().length; 

     var enableDisableButton = function(){   
     if(checkField > 0){ 
      $('#sendButton').removeAttr("disabled"); 
     } 
     else { 
      $('#sendButton').attr("disabled","disabled"); 
     } 
     }   

     //calling enableDisableButton() function on load 
     enableDisableButton();    

     $('input#message').keyup(function(){ 
     //checking the length of the value of message and assigning to the variable(checkField) on keyup 
     checkField = $("input#message").val().length; 
     //calling enableDisableButton() function on keyup 
     enableDisableButton(); 
     }); 
    }); 
    </script> 
    </body> 
</html> 
3

簡単な方法を試してください:CoffeeScriptのを使用するものについては

function toggleButton(ref,bttnID){ 
    document.getElementById(bttnID).disabled= ((ref.value !== ref.defaultValue) ? false : true); 
} 


<input ... onkeyup="toggleButton(this,'bttnsubmit');"> 
<input ... disabled='disabled' id='bttnsubmit' ... > 
1

、私が置かれています最も広く使用されているフォームの送信ボタンを無効にするためにグローバルに使用するコードです。上記のAdilの答えの適応。

$('#new_post button').prop 'disabled', true 
$('#new_post #post_message').keyup -> 
    $('#new_post button').prop 'disabled', if @value == '' then true else false 
    return 
関連する問題