在一个单击处理程序中执行2个函数

时间:2013-05-01 17:17:00

标签: javascript jquery

如何同时执行2个功能。在下面带有onclick的代码片段中,我得到父Div的id,然后使用id执行函数,

<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)
            }
        });
    }
});

1 个答案:

答案 0 :(得分: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);
});