[解決済み] Bootstrapの入力フィールド付きチェックボックス
2022-03-10 09:15:57
質問
例えば、「どの色が一番好きですか」という質問です。
What is your favorite color?
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id1">
<label class="custom-control-label" for="id1">Red</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id2">
<label class="custom-control-label" for="id2">Green</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id3">
<label class="custom-control-label" for="id3">Blue</label>
</div>
ADD "OTHER" INPUT HERE
その横に入力フィールドを追加して、"Other"のオプションを追加するにはどうすればよいですか?ありがとうございます!
どのように解決するのですか?
私の例 コードプリー
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
What is your favorite color?
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id1">
<label class="custom-control-label" for="id1">Red</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id2">
<label class="custom-control-label" for="id2">Green</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id3">
<label class="custom-control-label" for="id3">Blue</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id4">
<label class="custom-control-label" for="id4">Other</label>
<input type="text" placeholder="Custom Color Choice">
</div>
jqueryを少し使って、テキスト入力の値で自動的にチェックボックスにチェックを入れるように拡張しています。
$("#myTextBox").on("keyup", function() {
if ($("#myTextBox").val().length === 0) {
$('#id4').prop('checked', false);
} else {
$('#id4').prop('checked', true);
}
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
What is your favorite color?
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id1">
<label class="custom-control-label" for="id1">Red</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id2">
<label class="custom-control-label" for="id2">Green</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id3">
<label class="custom-control-label" for="id3">Blue</label>
</div>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="id4">
<label class="custom-control-label" for="id4">Other</label>
<input id="myTextBox" type="text" placeholder="Custom Color Choice">
</div>
関連
-
[解決済み】JavaScriptで相対URLへのリダイレクトを行う
-
[解決済み] jQueryでチェックボックスに "checked "を設定する
-
[解決済み] JavaScriptで現在のURLを取得する?
-
[解決済み] JavaScriptで要素のクラスを変更するにはどうすればよいですか?
-
[解決済み] Webフォームのフィールド/入力タグでブラウザのオートコンプリートを無効にするにはどうすればよいですか?
-
[解決済み] jQueryでページを更新するにはどうすればよいですか?
-
[解決済み] jQueryで要素にスクロールする
-
[解決済み] jQueryで入力を無効化/有効化する?
-
[解決済み] チェックボックスとそのラベルをクロスブラウザーで一貫して揃える方法
-
[解決済み】HTML5入力のプレースホルダの色をCSSで変更する。
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
[解決済み】Heroku:ノードアプリで「このアプリにはデフォルトの言語が検出されませんでした」エラーがスローされる
-
[解決済み】 Uncaught TypeError: data.push is not a function
-
[解決済み] Uncaught TypeError: 未定義のプロパティ 'top' を読み込めない
-
[解決済み】「Uncaught TypeError: Chromeで "Illegal invocation "が発生する。
-
[解決済み】BootstrapのCollapseが折りたたまれない
-
[解決済み】ある要素を別の要素に移動させるには?
-
[解決済み】SyntaxError: JSON の位置 1 に予期しないトークン o があります。
-
[解決済み】XMLパースエラー:ルート要素が見つからない コンソールの場所 FF
-
[解決済み】 \u003C とは何ですか?
-
[解決済み] [Solved] Uncaught Invariant Violation: 前のレンダリング中よりも多くのフックをレンダリングする