フォームでチェックしないと入力や送信ができないようにする

テキスト入力や送信ボタンを、特定の条件時に入力や送信ができないようにしてみます。

サンプルコード

HTML

<form action="">
	<div class="form-item">
		<input type="checkbox" name="other" id="other-check" />その他
		<input type="text" name="other-text" id="other-text" />
	</div>
	<div class="form-item">
		<input type="checkbox" name="agree" id="agree-check" />利用規約に同意する
	</div>
	<div class="btn">
		<input type="submit" value="送信" id="submit-btn" />
	</div>
</form>

JavaScript

$(function() {
	otherText();
	agreeText();

	$(':checkbox').change(function(){
		otherText();
		agreeText();
	});
});

function otherText() {
	if($('#other-check').is(':checked')) {
		$('#other-text').prop('disabled', false);
	} else {
		$('#other-text').prop('disabled', true);
	}

}
function agreeText() {
	if($('#agree-check').is(':checked')) {
		$('#submit-btn').prop('disabled', false);
	} else {
		$('#submit-btn').prop('disabled', true);
	}
}

ページアクセス時とチェックボックス変更時に、それぞれのチェックボックスにチェックが入っているかどうかを確認します。
チェックが入っていた場合はdisabledを追加、チェックが入っていない場合はdisabledを削除しています。
入力や送信ができないようにするデモページ
 

【参考サイト】

 

このエントリーをはてなブックマークに追加

関連記事

コメントを残す

メールアドレスが公開されることはありません。
* が付いている欄は必須項目です

CAPTCHA


コメントが承認されるまで時間がかかります。

2024年4月
 123456
78910111213
14151617181920
21222324252627
282930