2016-07-07 8 views
0

jqueryのバリデーションをいくつか行いました。ユーザーがinputから出て何かを書きませんでしたが、一部のテキストがplaceholderと表示されます。プレースホルダは表示されません

私はそれはクロムのdevのツール chrome dev toolに動作しますが、それはページ上に表示doesnの `tこのコード

$('input[type="text"]').on('blur', function() { 
    if (!!$(this).val()) { 
     $(this).css({ 
      'border': '#ff8383 1px solid' 
     }); 
     $(this).attr('placeholder', 'this field is required'); 
    } 
    else { 
     $(this).css({ 
      'border': '#c3c3c3 1px solid' 
     }); 
     $(this).attr('placeholder', ''); 
    } 
}); 

を書きました。

答えて

1

あなたはここに余分な!持っている:私は「単一書い

!!$(this).val() 

を、あなたは同じ(this)まず

$('input[type="text"]').on('blur', function() { 
 
    if (!$(this).val()) { 
 
    $(this).css({ 
 
     'border': '#ff8383 1px solid' 
 
    }).attr('placeholder', 'this field is required'); 
 
    } else { 
 
    $(this).css({ 
 
     'border': '#c3c3c3 1px solid' 
 
    }).attr('placeholder', ''); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<input type="text" placeholder="test" /> 
 
<input type="text" placeholder="" /> 
 
<input type="text" />

+0

attrcssに参加することができます! "、しかし、ステートメントが機能しなかった場合それは私のhtmlで値= ""があったからです。あなたは私に手がかりをくれました。 –

+0

あなたが見てみたいと思ったら答えを更新しました。 – dippas

1

これは問題の可能性があります。 if文に2つのbangがあります。

if (!!$(this).val()) { 
関連する問題