2017-07-31 45 views
1

如何通过名称获得iCheck电台价值,如$("input[type=radio][name=test]").val()?似乎它不起作用。请参阅JSFiddleJavascript:通过名称获得iCheck电台价值

HTML

<div class="i-checks"><label> <input type="radio" checked="" value="a" name="test"> a </label></div> 
<div class="i-checks"><label> <input type="radio" value="b" name="test"> b </label></div> 
<div class="i-checks"><label> <input type="radio" value="c" name="test"> c </label></div> 


The <span id='outputbythis' style='color:green'></span> is checked by $(this).val() 
<br> 
The <span id='outputbyname' style='color:red'></span> is checked by $("input[type=radio][name=test]").val() 

JS

回答

1

这是因为选择将返回数组。

jQuery(document).ready(function ($) { 

    $('input').iCheck({ 
     checkboxClass: 'icheckbox_flat-green', 
     radioClass: 'iradio_flat-green' 
    }); 



    $('input').on('ifToggled', function (event) { 

     $("#outputbythis").text($(this).val()) 

     $("#outputbyname").text($("input[type=radio][name=test]:checked").val()) 

    }); 

}); 

这应该工作

+0

如何通过名称获得I检查收音机的价值? – WCMC

+0

查看我的回复,我更新了 – Klajdi

+0

谢谢!真的很难找到一个自学者。我的意思是我无法理解和意识到,有时''(“input [type = radio] [name = test]”)'有时会在'$(“input [type = radio] [name = test]”)改为'$(“input [type = radio] [name = test]:checked”)。val()'。 – WCMC