2016-09-16 30 views
0

我使用一些代码来显示单选按钮的图片:单选按钮值不是在IE正确的。在其他浏览器中工作

HTML:

<div id="skin_1" title="Walk"> 
    <input type="radio" name="travel_mode" id="mode_walking" value="WALKING" /><br> 
    Walk 
</div> 

<div id="skin_2" title="Drive"> 
    <input type="radio" name="travel_mode" id="mode_driving" value="DRIVING" /><br> 
    Drive 
</div> 

<div id="skin_3" title="Bike"> 
    <input type="radio" name="travel_mode" id="mode_bicycle" value="BICYCLING" /><br> 
    Bike 
</div> 

<div id="skin_4" title="Transit"> 
<input type="radio" name="travel_mode" id="mode_transit" value="TRANSIT" /><br> 
Transit 
</div> 

<div> 
    <a href="#" onClick="testMe();">What mode is selected?</a> 
</div> 

JS:

$(function() { 
    $('input:radio').hide().each(function() { 
     var label = $("label[for=" + '"' + this.id + '"' + "]").text(); 
     $('<a ' + (label != '' ? 'title=" ' + label + ' "' : '') + ' class="radio-fx ' + this.name + '"><span class="radio' + (this.checked ? ' radio-checked' : '') + '"></span></a>').insertAfter(this); 
    }); 
    $('.radio-fx').on('click', function (e) { 
     $("input[name=travel_mode]").removeAttr("checked"); // add this line 
     $check = $(this).prev('input:radio'); 
     var unique = '.' + this.className.split(' ')[1] + ' span'; 
     $(unique).attr('class', 'radio'); 
     $(this).find('span').attr('class', 'radio-checked'); 
     this.blur(); 
     this.focus(); 
     $check.attr('checked', true); 
     getDirections(); 
    }).on('keydown', function (e) { 
     if ((e.keyCode ? e.keyCode : e.which) == 32) { 
      $(this).trigger('click'); 
     } 
    }); 
}); 

此代码在除IE以外的所有浏览器中均正常工作。在IE中,第二次点击一个单选按钮后,该值为“未定义”。

我试着使用:

this.blur(); 
this.focus(); 

按照其他建议,但没有奏效。

我该如何解决这个问题,以在IE中工作呢?

Here is the fiddle

+0

使用,而不是托attr的 –

+0

谢谢!这在IE中有效!请让这个答案。是什么使这项工作? – cpcdev

+0

实际上你只是在html中设置单选按钮的属性,它只会改变布局,但许多浏览器在标签的属性和状态上工作单选按钮就是其中一个,当你检查代码并改变广播状态时,你可以检查它按钮它不会追加任何属性,而是改变属性。 –

回答

1

使用道具,而不是ATTR

$(function() { 
     $('input:radio').hide().each(function() { 
      var label = $("label[for=" + '"' + this.id + '"' + "]").text(); 
      $('<a ' + (label != '' ? 'title=" ' + label + ' "' : '') + ' class="radio-fx ' + this.name + '"><span class="radio' + (this.checked ? ' radio-checked' : '') + '"></span></a>').insertAfter(this); 
     }); 
     $('.radio-fx').on('click', function (e) { 
      $("input[name=travel_mode]").prop('checked', false);; // add this line 
      $check = $(this).prev('input:radio'); 
      var unique = '.' + this.className.split(' ')[1] + ' span'; 
      $(unique).attr('class', 'radio'); 
      $(this).find('span').attr('class', 'radio-checked'); 
      this.blur(); 
      this.focus(); 
      $check.prop('checked', true); 
      getDirections(); 
     }).on('keydown', function (e) { 
      if ((e.keyCode ? e.keyCode : e.which) == 32) { 
       $(this).trigger('click'); 
      } 
     }); 
    }); 
相关问题