【问题标题】:JQuery-UI radio buttons update after F5F5 后 JQuery-UI 单选按钮更新
【发布时间】:2012-04-25 13:52:05
【问题描述】:

我正在使用 JQuery-UI 来显示像 here 这样的单选按钮。

我有以下 HTML 代码:

<div id="radioDiv">
    <input type="radio" id="u1-radio" name="radio" value="" checked/>
        <label for="u1-radio">U1</label>
    <input type="radio" id="u2-radio" name="radio" value="" />
        <label for="u2-radio">U2</label>
    <input type="radio" id="g1-radio" name="radio" value="" />
        <label for="g1-radio">G1</label>
</div>

以及以下 JavaScript:

$(document).ready(function(){
    $("#radioDiv").buttonset();
    getUseCase("u1");
}

function getUseCase(useCase){
    if( useCase == "u1" ){
        $("#u2-radio").checked = false;
        $("#g1-radio").checked = false;
        $("#u1-radio").checked = true;
        $("#radioDiv").buttonset('refresh');
    }
    if( useCase == "u2" ){
        $("#u1-radio").checked = false;
        $("#g1-radio").checked = false;
        $("#u2-radio").checked = true;
        $("#radioDiv").buttonset('refresh');
    }
    if( useCase == "g1" ){
        $("#u1-radio").checked = false;
        $("#u2-radio").checked = false;
        $("#g1-radio").checked = true;
        $("#radioDiv").buttonset('refresh');
    }
}

按钮工作正常,但问题是当我使用 F5 键刷新页面时:选中的按钮仍然是最后一个,因此“radioDiv”不会在选中第一个按钮时刷新。

我不明白为什么..刷新不应该召回$(document).ready(function(){})?

【问题讨论】:

标签: jquery jquery-ui radio-button


【解决方案1】:

要选择单选按钮值,请使用:

.attr('checked', true); // jQuery < 1.6
.prop('checked', true); // jQuery >= 1.6

而不是

.checked = true;

查看我的 jsFiddle,它可以工作:

http://jsfiddle.net/EPp2N/

【讨论】:

  • 与.prop('checked', true); 一起工作!我已经尝试过使用.attr('checked', true);,但是使用 jquery-1.7.2:我不知道它已被弃用。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2012-04-06
  • 2017-06-03
  • 1970-01-01
相关资源
最近更新 更多