2013-05-01 74 views
0

如何在同一時間執行2個功能。在下面的onclick的代碼片段,我得到父Div的id,然後使用id執行功能,在一個點擊處理程序中執行2個功能

<div id="selDe"> 
<input type="radio" class="ger" id="num1" checked><label for="num1"> 
<input type="radio" class="ger" id="num2"><label for="num2"> 
</div> 
<div id="selRe" > 
<input type="radio" class="ger" id="num1" checked><label for="num1"> 
<input type="radio" class="ger" id="num2"><label for="num2"> 

JS:

<script> 
$(".ger").on({ 
    click:function(){ 
     var pid = $(this).parent().attr("id"); //get the name of the div parent 
     $("#"+pid+" .ger").on({ 
//execute the function, but it just works after the next click 
      click: function() { 
       var showV = this.id.slice(3); 
       alert(showV) 
      } 
     }); 
    } 
}); 

+0

我不認爲這是可能的在JS – Adrift 2013-05-01 17:17:56

+1

ID應該始終是獨一無二的....這就是爲什麼它被稱爲「ID」 – bipen 2013-05-01 17:19:13

+2

我認爲你的意思是「在一個點擊處理程序」,而不是「在同一時間」。這是完全可能的。 – 2013-05-01 17:19:42

回答

1

試試這個:

$('div[id^=sel]').on('click', '.ger, div[id^=sel] .ger', function() { 
    var pid = $(this).parent().attr("id"); 
    alert(pid); 
    var showV = this.id.slice(3); 
    alert(showV); 
}); 
+1

謝謝,它工作正常! – Kakitori 2013-05-01 17:28:55

相關問題