jQuery Mobile-以编程方式更改单选按钮状态

jQuery Mobile - Change radiobutton state programmatically

本文关键字:单选按钮 状态 方式更 编程 Mobile- jQuery      更新时间:2023-09-26

在水平控制组中有此单选按钮:

<div data-role="fieldcontain">
 <fieldset data-role="controlgroup" data-type="horizontal">
   <legend>Geslacht:</legend>
   <input id="gender-male" name="gender" type="radio" value="MALE" />
   <label for="gender-male">Man</label>
   <input id="gender-female" name="gender" type="radio" value="FEMALE" />
   <label for="gender-female">Vrouw</label>
 </fieldset>
</div>

在给定的点上,我想通过编程重置值,使用:

$('#gender-male').prop('checked', false)
$('#gender-female').prop('checked', false)

但是,单选按钮的样式没有改变。

例如,如果选择了MALE,则它仍然显示为已选择。

我应该刷新一下吗?

看看这个:

$('#gender-male').attr("checked",false).checkboxradio("refresh");

JqueryMobile 1.4的工作解决方案(已测试)如下:

(html取自JQM 1.4演示代码)

<form><fieldset data-role="controlgroup">
    <input type="radio" name="radio-choice-v-2" id="radio-choice-v-2a" value="on" checked="checked">
    <label for="radio-choice-v-2a" style="font-weight: 100;">radio button A</label>
    <input type="radio" name="radio-choice-v-2" id="radio-choice-v-2b" value="off">
    <label for="radio-choice-v-2b" style="font-weight: 100;">radio button B</label>
    </fieldset></form>

如上所述,选中单选按钮A。您要将单选按钮B设置为选中,将A设置为未选中。请注意:复选框xradio("刷新")用于每个单选按钮。

Javascript/Jquery(取自JQM 1.4 API文档并重新改编)

    if (radioSetting == 0) {
        $( "#radio-choice-v-2b" ).prop( "checked", false ).checkboxradio( "refresh" );
        $( "#radio-choice-v-2a" ).prop( "checked", true ).checkboxradio( "refresh" );
    } 
    else {
        $( "#radio-choice-v-2a" ).prop( "checked", false ).checkboxradio( "refresh" );
        $( "#radio-choice-v-2b" ).prop( "checked", true ).checkboxradio( "refresh" );
    }