1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

jQuery テキストエリアに文字が入力されたらsubmitボタンを有効にする

Posted at

経緯

Laravelで検索機能を実装しようとしてキーワードが入力されていないときのバリデーションをどうしようかと思ったけどボタンを押せなくすればいいじゃん。

多分同じ記事は腐るほどあると思うけど自分みたいなLaravel初心者がいるとしてここにたどり着いてくれるように祈りを込めて。(備忘録でもある)

HTML

<form action="#" method="GET">
    <input type="text" id="key_word_input" name="key_word" placeholder="キーワードを入力してください">
    <button type="submit" id="search_button">検索</button>
</form>

jQuery

テキストボックスに変更があるたびにvalue属性を調べてdisabled属性を切り替える。

var keyWord = $('#key_word_input');
var searchButton = $('#search_button');

$(searchButton).prop('disabled', true);

$(keyWord).on('change', function() {
    if($(keyWord).val()) {
        $(searchButton).prop('disabled', false);
    } else {
        $(searchButton).prop('disabled', true);
    }
});

おわりに

調べなくてもちょっとずつjQuery書けるようになってきて嬉しい。

ちなみに肝心の検索機能はまだできていない。
できたらまたメモするかもしれないです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?