2014-02-17 99 views
0

如何根据单选按钮选择做一个简单的if语句?jQuery单选按钮if语句

因此,如果单选按钮英寸显示,然后显示div.inchesUser(使用.show) 而厘米相反。

这就是我想要的功能做:

$('div.inchesUser').show(200); 

这是我的单选按钮代码:

<form id="unitChooser"> 
    <label>Inches or centimetres?</label> 
    <input class="unitSelectInches" type="radio" name="units" value="inches" />Inches 
    <input class="unitSelectCenti" checked="checked" type="radio" name="units" value="cms" />Centimetres 
</form> 
+0

可能重复http://stackoverflow.com/questions/596351/how-can-i-get- which-radio-is-selected-via-jquery) – Barmar

回答

0

试试这个

$("input:radio[name=units]").click(function() { 
    var value = $(this).val(); 
    if(value == 'inches') 
     $('div.centimetreUser').show(200); 
}); 

DEMO

+0

你不应该在'if'中包含所有的东西。 – Barmar

+0

似乎没有工作:http://jsfiddle.net/8pEp2/7/ – Francesca

+0

检查此jsfiddle我有一个更新:http://jsfiddle.net/8pEp2/8/ @Francesca –

0

东西像这样 -

<form id="unitChooser"> 
    <label>Inches or centimetres?</label> 
    <input class="unitSelectInches" data-class='inchesUser' type="radio" name="units" value="inches" />Inches 
    <input class="unitSelectCenti" data-class='centimetresUser' checked="checked" type="radio" name="units" value="cms" />Centimetres 
</form> 

$('input:radio[name=units]').on('change', function() { 
    $('div.centimetreUser, div.inchesUser').hide(); 
    $('div.' + $(this).data('class')).show(200); 
}); 

演示--->http://jsfiddle.net/WT2uc/

的[我怎样才能获得通过jQuery选择哪种广播?(
+0

似乎无法得到这个工作? http://jsfiddle.net/8pEp2/10/ – Francesca

+0

@Francesca看到这个演示' - >'http://jsfiddle.net/WT2uc/ ------你的小提琴更新' - >'http:/ /jsfiddle.net/8pEp2/12/ –