2017-07-31 65 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