jQueryを使い、テキストフィールド(input type=”text”)に文字を入力するとボタンが有効になり、削除するとボタンが無効になるプログラムを作成。
$(function() {
document.getElementById("btn").disabled = "true";
$('#input').on('keyup', function() {
if( $('#input').val().length > 0 ) {
$("#btn").prop('disabled', false);
} else {
$("#btn").prop('disabled', true);
}
});
});
HTMLは、下のコードになります。
<input type="text" id="input"></input> <button id="btn">ボタン</button>
スポンサーリンク

コメント