アプリ版:「スタンプのみでお礼する」機能のリリースについて

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にする方法を教えてください。

A 回答 (2件)

refresh が必要です。



$('#chk1').prop('checked', true).checkboxradio('refresh');
    • good
    • 0
この回答へのお礼

ありがとうございました。反映されました!

お礼日時:2016/05/26 19:34

>ある条件で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"]')
は、ちょっとおかしいかも・・・
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!