2011-04-28 41 views
38

我有一个应用程序,当某个单选按钮被选中时显示一个div,然后在未选中单选按钮时隐藏该div。问题在于单选按钮附加的更改事件仅在选中单选按钮时被调用,而不是在选中另一个时(不选中前一个)。我当前的代码如下:监控何时单选按钮未被选中

<form name="newreport" action="#buildurl('report.filters')#" method="post"> 
    <dl class="oneColumn"> 
     <dt class="first"><label for="txt_name">Name</label></dt> 
     <dd><input type="text" name="name" id="txt_name" class="text" /></dd> 
     <dt><strong>Type</strong></dt> 
     <dt><input type="radio" name="type" id="rdo_list" value="list" checked="checked" /><label for="rdo_type" style="display:inline;">List</label></dt> 
     <dd>List a group of records</dd> 
     <dt><input type="radio" name="type" id="rdo_fields" value="fields" /><label for="rdo_fields" style="display:inline;">Field Breakdown</label></dt> 
     <dd>Breaks down distinct field values for comparison</dd> 
     <dt><input type="radio" name="type" id="rdo_history" value="history" /><label for="rdo_history" style="display:inline;">Historical Comparison</label></dt> 
     <dd>Provides record changes over a group of years for growth comparisons</dd> 
     <dt><input type="radio" name="type" id="rdo_breakdown" value="breakdown" /><label for="rdo_breakdown" style="display:inline;">Student Breakdown</label></dt> 
     <dd>Breaks down students by school, district or grade</dd> 
     <div class="reportyear"> 
      <dt><label for="txt_year">Year to Report</label></dt> 
      <dd><input type="text" name="year" id="txt_year" class="text" value="#year(Rc.yearstart)#" /></dd> 
     </div> 
     <div class="date-spans" style="display:none;"> 
      <dt><label for="txt_yearstart">Year Start</label></dt> 
      <dd><input type="text" name="yearstart" id="txt_yearstart" class="text" value="#evaluate(year(Rc.yearstart)-5)#" /></dd> 
      <dt><label for="txt_yearend">Year End</label></dt> 
      <dd><input type="text" name="yearend" id="txt_yearend" class="text" value="#year(Rc.yearstart)#" /></dd> 
     </div> 
    </dl> 
</form> 


<script type="text/javascript"> 
    $(function(){ 
     $('##rdo_history').change(function(e){ 
      var isChecked = $(this).attr(checked); 
      var $datespan = $('form .date-spans'); 
      var $year = $('form .reportyear'); 

      if(isChecked){ 
       $year.css({display:'none'}); 
       $datespan.fadeIn('fast'); 
      }else{ 
       $datespan.css({display:'none'}); 
       $year.fadeIn('fast'); 
      } 
     }); 
    }); 
</script> 

这似乎是一个非常简单的脚本,但该事件仅被调用的检查事件,而不是在取消选中事件。谁能告诉我为什么?

+0

如果你想要单选按钮的“当前”值(相对于加载HTML时的值),那么你必须**使用jQuery'.val()'而不是'.attr()',参见http://stackoverflow.com/questions/8312820/jquery-val-vs-attrvalue – 2014-07-07 11:52:13

回答

61

处理收音机组上的更改事件,而不是每个按钮。然后看看哪一个被检查。

下面是一些未经测试的代码,希望显示了我说的:)...

$("input[name='type']").change(function(e){ 
    if($(this).val() == 'history') { 
     $year.css({display:'none'}); 
     $datespan.fadeIn('fast'); 
    } else { 
     $datespan.css({display:'none'}); 
     $year.fadeIn('fast'); 
    } 

}); 
+1

检查单选按钮是否被选中的更通用的方法是if($(this).is(':checked')) ',请参阅http://stackoverflow.com/questions/2272507/find-out-if-radio-button-is-checked-with-jquery – 2014-07-07 11:54:19

+4

@AdrienBe,我不相信在监视单选按钮集合时工作。你需要知道选择哪一个。 – Brian 2014-07-07 15:54:00

+0

这是行不通的,但是下面将会:'$(“input:radio [name ='managerelradio']:checked”)。val();' 参见:http://stackoverflow.com/questions/ 7723505 /获取 - 值从无线电基团的使用,jquery的 – tolmark 2016-04-28 16:08:30

7

注意此行为时获得无线电输入值:

$('input[name="myRadio"]').change(function(e) { // Select the radio input group 

    // This returns the value of the checked radio button 
    // which triggered the event. 
    console.log($(this).val()); 

    // but this will return the first radio button's value, 
    // regardless of checked state of the radio group. 
    console.log($('input[name="myRadio"]').val()); 

}); 

所以$('input[name="myRadio"]').val()不会像您所期望的那样返回无线电输入的检查值 - 它会返回第一个单选按钮的值。