2016-11-08 187 views
1

我試圖使用onclick事件與引導單選按鈕來獲取名稱和屬性,但一些它如何不順利。onclick無法正常工作與引導

一些無線電按鈕觸發事件,但沒有得到名稱或類(「未定義」),有些根本沒有響應。

<div class="theContainer">  
    <input type="radio" class="myClass" name="options" id="option1" onclick="updateODid()" autocomplete="off" > Radio 1 
    <input type="radio" class="myClass" name="options" id="option2" onclick="updateODid()" autocomplete="off"> Radio 2 
    <input type="radio" class="myClass" name="options" id="option3" onclick="updateODid()" autocomplete="off"> Radio 3 
    <input type="hidden" class="findme" value="found me!"> 
    <div class="btn-group" data-toggle="buttons"> 
    <label class="btn btn-primary active"> 
     <input type="radio" class="myClass" name="options" id="option1"  onclick="updateODid()" autocomplete="off" > Radio 1 
    </label> 
    <label class="btn btn-primary"> 
     <input type="radio" class="myClass" name="options" id="option2" onclick="updateODid()" autocomplete="off"> Radio 2 
    </label> 
    <label class="btn btn-primary"> 
     <input type="radio" class="myClass" name="options" id="option3" onclick="updateODid()" autocomplete="off"> Radio 3 
    </label> 
    </div> 
</div> 

jsfiddle.net/2sc8mc7L/6/

謝謝!

回答

3

它不工作,因爲你有錯誤的方式混合了JavaScript和jQuery。

請檢查更新的摘要:https://jsfiddle.net/2sc8mc7L/12/ 的onclick功能應該是如下:

$(".myClass").on('click',function(){ 
    radioButtonClick($(this)); 
}); 

$("label.btn-primary").on('click',function(){ 
    radioButtonClick($(this).find('.myClass')); 
}); 

function radioButtonClick(_this){ 
    alert("I'm in!") 
    alert (_this.attr('name')); 
    alert (_this.attr('class')); 
    alert (_this.closest(".theContainer").find(".findme").val()); 
} 
+0

謝謝 - 但引導單選按鈕stil我不工作... :( –

+0

好吧,請檢查https://jsfiddle.net/2sc8mc7L/11/鏈接。我也實施了它。因此在bootsrap單選按鈕上的實際點擊是在標籤上,所以這就是爲什麼包含另一個jquery代碼。 –

+0

怎麼可能?我正按收音機!爲什麼標籤? –

1

你可以嘗試這樣的,使用updateODid(this)onclick,它會使用當前實例

function updateODid(button) 
 
\t { 
 
\t \t //alert("I'm in!") 
 
\t \t alert ($(button).attr('name')); 
 
    alert ($(button).attr('class')); 
 
    alert ($(button).closest(".theContainer").find(".findme").val()) 
 
\t }
<div class="theContainer"> 
 

 
    <input type="radio" class="myClass" name="options" id="option1" onclick="updateODid(this)" autocomplete="off" > Radio 1 
 
    
 
    <input type="radio" class="myClass" name="options" id="option2" onclick="updateODid(this)" autocomplete="off"> Radio 2 
 
    
 
    <input type="radio" class="myClass" name="options" id="option3" onclick="updateODid(this)" autocomplete="off"> Radio 3 
 

 

 
    <input type="hidden" class="findme" value="found me!"> 
 

 

 
    <div class="btn-group" data-toggle="buttons"> 
 
     <label class="btn btn-primary active"> 
 
     
 
    \t \t <input type="radio" class="myClass" name="options" id="option1"  onclick="updateODid(this)" autocomplete="off" > Radio 1 
 
     
 
     </label> 
 
     <label class="btn btn-primary"> 
 
     
 
\t  <input type="radio" class="myClass" name="options" id="option2" onclick="updateODid(this)" autocomplete="off"> Radio 2 
 
     
 
     </label> 
 
     <label class="btn btn-primary"> 
 
     
 
\t  <input type="radio" class="myClass" name="options" id="option3" onclick="updateODid(this)" autocomplete="off"> Radio 3 
 
     
 
     </label> 
 
    </div> 
 

 
</div><script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>