最终显示不正确的无线电值...无法弄清楚为什么

时间:2016-09-07 03:58:37

标签: javascript event-handling radio-button getelementbyid jquery-events

我正在使用一些代码将单选按钮显示为图像:

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 + '" href="#"><span class="radio' + (this.checked ? ' radio-checked' : '') + '"></span></a>').insertAfter(this);
    });
    $('.radio-fx').on('click', function (e) {
        $check = $(this).prev('input:radio');
        var unique = '.' + this.className.split(' ')[1] + ' span';
                $(unique).attr('class', 'radio');
                $(this).find('span').attr('class', 'radio-checked');
                $check.attr('checked', true);
            }).on('keydown', function (e) {
                if ((e.keyCode ? e.keyCode : e.which) == 32) {
                    $(this).trigger('click');
            }
    });
});

问题:

最后更改单选按钮并单击“选择了什么模式?”后有足够的时间警告错误的单选按钮值。

问题:

如何每次都能获得正确的单选按钮值?

这是小提琴:Live Fiddle Here

4 个答案:

答案 0 :(得分:2)

我刚添加了一行来清除所有复选框:

$('.radio-fx').on('click', function (e) {
   $("input[name=travel_mode]").removeAttr("checked"); // add this line
   .....

完整Fiddle

答案 1 :(得分:0)

在函数内定义变量,以便每次都重置。

function testMe() {

var selected_travel_mode = $("input[name=travel_mode]:checked").val();

alert(selected_travel_mode);

}

答案 2 :(得分:0)

选择新的时,你必须重置无线电检查状态。

    var pre;
            $('.radio-fx').on('click', function (e) {
                if(pre!==undefined)
                  $(pre).attr('checked',false);                   
                pre = $(this).prev('input:radio');
                $check = $(this).prev('input:radio');
                var unique = '.' + this.className.split(' ')[1] + ' span';
                $(unique).attr('class', 'radio');
                $(this).find('span').attr('class', 'radio-checked');
                $check.attr('checked', true);
            }).on('keydown', function (e) {
                if ((e.keyCode ? e.keyCode : e.which) == 32) {
                    $(this).trigger('click');
                }
            });
        });

答案 3 :(得分:0)

尝试做这样的工作示例:

&#13;
&#13;
function testMe() { 
  if ($("input:radio[name='travel_mode']").is(":checked")) {
    alert($("input:radio[name='travel_mode']:checked").attr("value"));
    // or
    alert($("input:radio[name='travel_mode']:checked").val());
  } else { 
    alert("You must make a choice!");
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
&#13;
&#13;
&#13;