2013-05-01 27 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

相关问题