JQuery Mobileのラジオボタンでハマる
jQuery-Mobile初心者なのですが、ラジオボタンの値をJSから取得・設定しようとして壮絶にはまったのでメモします。
sample.html
<div class="ui-field-contain">
<label for="pcschoice">ラジオボタンによる値設定</label>
<fieldset name="radiochoice" id="radiochoice" data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">
<input type="radio" name="radiochoice" id="radiochoice1" value="1" />
<label for="pcschoice1">1</label>
<input type="radio" name="radiochoice" id="radiochoice2" value="2" />
<label for="pcschoice2">2</label>
<input type="radio" name="radiochoice" id="radiochoice3" value="3" />
<label for="pcschoice3">3</label>
</fieldset>
</div>
sample.js
$(document).on("pagebeforeshow", "#serialportsetting", function () {
var p = $('#radiochoise input:radio');
var s = $('#sensorchoise input:radio');
for (var i = 0; i < 3; i++) {
if ($(document).data("sysinfo")['serial']['pcs'] == $(p[i]).val()) {
$(p[i]).attr("checked", true).checkboxradio("refresh");
}
if ($(document).data("sysinfo")['serial']['sensor'] == $(s[i]).val()) {
$(s[i]).attr("checked", true).checkboxradio("refresh");
}
}
});
ポイント
<form>
は特に必要ない