检查单选按钮值if if是true还是false并显示hide div

时间:2011-10-26 11:29:05

标签: jquery animation radio-button

我有2个单选按钮,我目前检查它们的值,如果是6或12并隐藏/显示我表中的相关div。

<tr>   
   <td>
       <div class="Price6 band" style="display: none;">21.09</div>
       <div class="Price12 band">21.09</div>
   </td>
</tr>
<tr>
      <td>
        <input id="radio1" class="radio" type="radio"  name="price" value="6">
         <label for="radio1">£24.95</label>
          <input id="radio1" class="radio" type="radio" name="price" value="12">
          <label for="radio1">&nbsp;£6.95</label>
      </td>
 </tr>

如何编辑我的jquery代码,以便检查单选按钮的值,如果为true或false,而不是数字值?

我的jquery目前是

$(document).ready(function() {
    $("input[name$='price']").click(function() {//switch bewtween price bands
        var test = $(this).val();

        $("div.band").hide();
        $(".Price" + test).show();
    });
});

2 个答案:

答案 0 :(得分:2)

你可以做这样的事情

$("input").change(function(){
    var test = $(this).val();
    if(test == '6'){
        $(".Price6").show();
        $(".Price12").hide();
    }
    if(test == '12'){
        $(".Price12").show();
        $(".Price6").hide();
    }
});

示例: http://jsfiddle.net/QsXm5/

注意:我更改了显示价格div的#s,因为在您的示例中它们是相同的,您无法判断是否发生了更改。

答案 1 :(得分:0)

您可以将“$(this).val()”替换为“$(this).attr(”checked“)”将返回“选中”此选择此单选按钮,或者如果不选择则返回“未定义”,则如果需要,您可以使用此值来运行脚本或将其替换为true / false。