2012-05-12 100 views
-2
<input type="text" id="textbox"> 
<input type="radio" id="radio1" name="on"> 
<input type="radio" id="radio2" name="off"> 

我只在#radio1被选中(第一页面加载)时才需要显示#textbox。或#radio2在第一页加载时被选中,并且用户点击#radio2文本框也应该显示。jquery检查收音机是否被选中或是否被检查

任何建议如何做到这一点?

+1

如何你的意思是*的第一个页面加载?*是通常有多个页面加载? :) –

+0

我不确定我是否理解你的问题,你能解释一些吗? – gdoron

+0

你什么时候隐藏文本框? –

回答

1

demo

if($('input#radio1').is(':checked')){ 
    $('#textbox').show(); 
} 

$('input#radio1, input#radio2').on('change',function(){ 
    $('#textbox').show(); 
}); 
1

如果我得到你想要的东西的权利:

$(function(){ 
    if ($('#radio1').is(':checked')){ // if the radio1 is checked when the page 
     alert($('#textbox').val()); // was loaded, display textbox value 
    } 

    $('#radio2').click(function(){ // When the radio2 is clicked 
     alert($('#textbox').val()); // show textbox value. 
    }); 
}); 
+0

我想隐藏/显示文本框,但谢谢,我可以从这里自己做。很有帮助。 – domino

+0

@domino。好的,所以你可以编辑我想要的答案。 – gdoron

0
$(function(){ 
    $('#radio1').on('click', function() { 
     if(this.checked) { 
     $('#textbox').show(); 
     } else $('#textbox').hide(); 
    }).click(); // firing a initial click 

    $('#radio2').on('click', function() { 
     $('#textbox').show(); 
    }); 
});