htmlで以下のようにチェックボックスを設定します。
<input type="checkbox" name="chk1" id="chk1" value="1">
<label for="chk1">チェック1</label>
<input type="checkbox" name="chk2" id="chk2" value="1">
<label for="chk2">チェック2</label>
そこで、ある条件でchk1をONにしようと思い、Javascirptには以下のように記述しました。
$('#chk1').prop('checked', true);
ところが、chk1はONになりません。デバッガでも上記処理が通ることを確認しています。
なお、Javascriptを以下のように書き直しても動きません。
$('#input[name="chk1"]').prop('checked', true);
ちなみに、labelタグをはずすとちゃんとチェックがONになります。
labelがある状態でチェックをONにする方法を教えてください。
No.2ベストアンサー
- 回答日時:
refresh が必要です。
$('#chk1').prop('checked', true).checkboxradio('refresh');
No.1
- 回答日時:
>ある条件でchk1をONにしよう
その条件が間違っているのでは?
普通に行けると思いますが・・
<script>
$(function() {
$('#chk3').click(function(){
$('#chk1').prop('checked', true);
$('[name="chk2"]').prop('checked', true);
});
});
</script>
<input type="checkbox" name="chk1" id="chk1" value="1">
<label for="chk1">チェック1</label>
<input type="checkbox" name="chk2" id="chk2" value="1">
<label for="chk2">チェック2</label>
<input type="button" id="chk3" value="chk3">
ちなみに
>$('#input[name="chk1"]')
は、ちょっとおかしいかも・・・
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・14歳の自分に衝撃の事実を告げてください
- ・架空の映画のネタバレレビュー
- ・「お昼の放送」の思い出
- ・昨日見た夢を教えて下さい
- ・【お題】絵本のタイトル
- ・【大喜利】世界最古のコンビニについて知ってる事を教えてください【投稿~10/10(木)】
- ・メモのコツを教えてください!
- ・CDの保有枚数を教えてください
- ・ホテルを選ぶとき、これだけは譲れない条件TOP3は?
- ・家・車以外で、人生で一番奮発した買い物
- ・人生最悪の忘れ物
- ・【コナン30周年】嘘でしょ!?と思った○○周年を教えて【ハルヒ20周年】
- ・ハマっている「お菓子」を教えて!
- ・最近、いつ泣きましたか?
- ・夏が終わったと感じる瞬間って、どんな時?
- ・10秒目をつむったら…
- ・人生のプチ美学を教えてください!!
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・都道府県穴埋めゲーム
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
背景色を変えて未入力チェック...
-
チェックボックスの設定
-
特定のID(またはクラス)で括ら...
-
JSP内で可変するチェックボック...
-
配列のチェックボックスをjavas...
-
if文でelseを使わずに否定
-
確認ページからフォームページ...
-
選択したラジオボタンが画面の...
-
return trueとreturn falseの用...
-
チェックボックス付きのテーブ...
-
Selectボックスの一覧表示方法
-
onClickとsubmitの処理順序
-
onchangeイベントを強制的に発...
-
マウスをブラウザの外に出した...
-
formのfileの値をhiddenでも持...
-
【jQuery】input nameの文字列...
-
javascriptで .jpg , .jpeg , ....
-
ボタンを押すとチェックボック...
-
二つの入力欄に、同時に同じ文...
-
JavaScriptでHTML内に記入され...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
チェックボックスのON/OFFでVal...
-
チェックボックスのON/OFFに応...
-
【jsp/Java】チェックボックス...
-
checkboxをクリックしてリロー...
-
チェックボックスで指定したも...
-
確認ページからフォームページ...
-
チェックボックスの設定
-
javascriptで確認ダイアログの...
-
フォームのチェックボックスの...
-
複数式のチェックボックス
-
背景色を変えて未入力チェック...
-
JQuery Mobileでチェックボック...
-
JSP内で可変するチェックボック...
-
チェックボックスが1つ以上チェ...
-
複数のチェックボックス項目が...
-
一方のチェックボックスのON/OF...
-
documentオブジェクトのlength...
-
チェックボックスの使い方
-
チェックボックスの無効化、有効化
-
Firefoxでテキストボックスから...
おすすめ情報