【问题标题】:Select a Radio Button in a Fieldset Programatically以编程方式选择字段集中的单选按钮
【发布时间】:2013-06-04 15:19:26
【问题描述】:

我有一个带有单选的字段集(问题的选项),我想在页面显示上以编程方式在选项控制组中预选一个单选按钮,这里是控制组:

<fieldset data-role="controlgroup" id="options">
    <input type="radio" name="radio-choice-1" id="optiona" value="a"
                                                           checked="checked" />
    <label for="optiona" id="labela">Ondo</label>
    <input type="radio" name="radio-choice-1" id="optionb" value="b" />
    <label for="optionb">Lagos</label>
    <input type="radio" name="radio-choice-1" id="optionc" value="c" />
    <label for="optionc">Abuja</label>
    <input type="radio" name="radio-choice-1" id="optiond" value="d" />
    <label for="optiond">Kogi</label>
    <input type="radio" name="radio-choice-1" id="optione" value="e" />
    <label for="optione">Niger</label>
</fieldset>

我尝试了以下方法:

var sel = questions[indexNo].correct;
$("#option" + sel).prop("checked", true)
$("#option"+ sel).is(":checked");

【问题讨论】:

  • 问题对象是什么样子的?
  • Questions 是一个数组对象,看起来像这个 question({'quest':"",'optionsa':"",'optionsb':"",'correct':""}),我不认为这很重要,因为我要做的是在用户再次导航到问题时跟踪用户每个问题选择的答案,他的选择应该被预先选择

标签: jquery jquery-mobile


【解决方案1】:

工作示例:http://jsfiddle.net/Gajotres/aawNj/

$(document).on('pagebeforeshow', '#index', function(){ 
    $( "#optiona" ).prop( "checked", false ).checkboxradio( "refresh" );    
    $( "#optionb" ).prop( "checked", true ).checkboxradio( "refresh" );
});

或另一个工作示例:http://jsfiddle.net/Gajotres/EFzxj/

$(document).on('pagebeforeshow', '#index', function(){ 
    $( "#optionb" ).prop( "checked", true );  
    $('#content').trigger('create');
});

【讨论】:

  • 这可行,但它不会清除先前选择的选项,因此您在控制组中选择了 2 个选项,如何清除控制组中所有选择的选项。
  • 仔细看看我的例子,他们都在清除前一个。第一个带有“checked”,false,第二个带有 trigger('create');当然,触发器创建必须在具有 data-role="content" 的 DIV 上执行。再次,看看我的例子,一切都在那里。
  • 在选择正确答案之前,我需要取消选择该组中的任何单选按钮
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2021-12-18
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多