Javascript 使用 Jquery Mobile 检查和取消选中单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14409571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:40:18  来源:igfitidea点击:

Checking and unchecking radio buttons with Jquery Mobile

javascripthtmlcordovajquery-mobileradio-button

提问by danielrvt-sgb

I can't check a set of checkboxes programatically with jquery mobile, I have the following code:

我无法使用 jquery mobile 以编程方式检查一组复选框,我有以下代码:

<div data-role="fieldcontain"  id="div_radio" class="radiogroup">
    <fieldset data-role="controlgroup">
        <input type="radio" name="radio-pieces" id="radio-choice-1" value="3" checked="checked" />
        <label for="radio-choice-1">1 to 3</label>

        <input type="radio" name="radio-pieces" id="radio-choice-2" value="5"  />
        <label for="radio-choice-2">4 to 5</label>

        <input type="radio" name="radio-pieces" id="radio-choice-3" value="6"  />
        <label for="radio-choice-3">over 5</label>
    </fieldset>
</div>

If I do: $("input[type='radio']:last").attr("checked",true).checkboxradio("refresh");everything works perfect, but none of this work at all:

如果我这样做:$("input[type='radio']:last").attr("checked",true).checkboxradio("refresh");一切正常,但这些都不起作用:

$("input[type='radio']:first").attr("checked",true).checkboxradio("refresh");
$("input[type='radio']:eq(0)").attr("checked",true).checkboxradio("refresh");
$("input[type='radio']:eq(1)").attr("checked",true).checkboxradio("refresh");
$("input[type='radio']:eq(2)").attr("checked",true).checkboxradio("refresh");

How can I properly manipulate these elements? Unselecting all checkboxes also works fine:

如何正确操作这些元素?取消选择所有复选框也可以正常工作:

$("input[type='radio']").attr("checked",false).checkboxradio("refresh");

It seems that the only checkbox working is the last one.

似乎唯一有效的复选框是最后一个。

回答by peterm

They all work just fine. You just need to trigger refreshon all input radioin group.

他们都工作得很好。你只需要触发refresh的所有input radio组。

$("input[type='radio']:first").attr("checked", "checked");
$("input[type='radio']").checkboxradio("refresh");

jsFiddle is here.

jsFiddle 在这里

回答by user1477388

Nothing worked for me except:

除了:

$('#reset').click(function(){
    $('#Store').trigger("click").trigger("click"); // yes... twice
});

On jQuery Mobile 1.4.2.

在 jQuery Mobile 1.4.2 上。

回答by japetko

For me works this to uncheck the whole group of radios:

对我来说,这可以取消选中整个收音机组:

$(".my_class").removeAttr("checked");
$(".my_class").checkboxradio("refresh");

回答by Jan Segre

This doesn't seem right. The single quote is not needed input[type=radio] is correct. I'm using an outdated version (1.1.1). It would be help to know what version you're using.

这似乎不对。不需要单引号 input[type=radio] 是正确的。我使用的是过时的版本 (1.1.1)。了解您使用的版本会有所帮助。

Keep in mind that those are radio buttons, only one selected at a time.

请记住,这些是单选按钮,一次只能选择一个。

回答by kathir

in Jquery mobile radio button refresh like this:

在 Jquery 移动单选按钮中刷新如下:

$(".iscfieldset input[type='radio']:first").attr("checked", "checked");


    $(".iscfieldset input[type='radio']").checkboxradio().checkboxradio("refresh"); 

try this working fine for me

试试这个对我来说很好用