使用其他无线电选择和刷新按钮切换无线电选择而不是页面

时间:2011-03-07 17:10:22

标签: jquery jquery-ui radio-button toggle jquery-mobile

好的我有一个表格,其中有一些问题作为无线电选项。如果选择第一个无线电作为选项是,则显示第二个无线电选项。如果第二个单选选项也被选为是选项,则显示第三个元素(文本框)。

此功能正在运行,但我无法工作的是,如果他们将第一个无线电选择更改为否,我怎么能隐藏和取消设置第二个无线电选项(将选项设置为否)并清除和隐藏第三个元素(文本框)值。

以下是我的尝试:

$("#second_radio_div").hide("fast");
$("#third_element_div").hide("fast");

$("[name=first_radio]").change(function(){
    $("#second_radio_div").toggle($("[name=first_radio]").index(this)===1);

    if($("[name=first_radio]").index(this)===0) {
        //$('input:radio[name="second_radio_no"]').attr('checked',true); 
        //$('#second_radio').buttonset("refresh");
        //$('[name="second_radio"][value="no"]').attr("checked", true);
        //$('#second_radio').buttonset("refresh");
        $("#third_element_div").hide("fast"); // This works to hide the element
    }
});

$("[name=second_radio]").change(function(){
    $("#third_element_div").toggle($("[name=second_radio]").index(this)===1);
});

HTML

<div data-role="fieldcontain">
<fieldset data-role="controlgroup" data-type="horizontal">
    <div>First Radio</div>
        <input type="radio" name="first_radio" id="first_radio_yes" value="yes" />
        <label for="first_radio_yes">Yes</label>

        <input type="radio" name="first_radio" id="first_radio_no" value="no" checked="checked"/>
        <label for="first_radio_no">No</label>
    </fieldset>
</div>


<div id="second_radio_div" name="second_radio_div">
    <div data-role="fieldcontain">
        <fieldset data-role="controlgroup" data-type="horizontal">
            <div>Second Radio</div>
            <input type="radio" name="second_radio" id="second_radio_yes" value="yes" />
            <label for="second_radio_yes">Yes</label>

            <input type="radio" name="second_radio" id="second_radio_no" value="no" checked="checked"/>
            <label for="second_radio_no">No</label>
        </fieldset>
    </div>
</div>

<div id="third_element_div" name="third_element_div">
    <div data-role="fieldcontain">
        <input type="text" name="third_element" id="third_element" class="default-value" value="If yes, provide date" />                
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

$("#second_radio_div, #third_element_div").hide();

$("[name=first_radio]").change(function() {
    var show = $(this).val() == "yes";
    $("#second_radio_div").toggle(show);

    if (!show) {
        $('#second_radio_no').attr('checked',true); 
        $("#third_element_div").hide();
    }
});

$("[name=second_radio]").change(function() {
    $("#third_element_div").toggle($(this).val() == "yes");
});

工作示例:http://jsfiddle.net/hunter/N6qmr/


我认为你所拥有的主要问题是这一行:

$('input:radio[name="second_radio_no"]').attr('checked',true);

应该是:

$('#second_radio_no').attr('checked', true);

由于没有[name="second_radio_no"]

的元素

答案 1 :(得分:1)

怎么样:

$("#second_radio_div").hide("fast");
$("#third_element_div").hide("fast");

$("[name='first_radio']").change(function() {
    $("#second_radio_div").toggle($("[name='first_radio']").index(this) === 0);

    if ($("[name='first_radio']").index(this) === 1) {
        $("#second_radio_no").attr("checked", true);
        $("#third_element_div").hide("fast"); // This works to hide the element
    }
});

$("[name='second_radio']").change(function() {
    $("#third_element_div").toggle($("[name='second_radio']").index(this) === 0);
});

在此工作:http://jsfiddle.net/dVAzj/2/

请注意attribute equals[name='value'])选择器中的'值'周围需要引号。

相关问题