用eval动态创建的jQuery选择器?

时间:2019-01-14 17:45:42

标签: javascript jquery for-loop onclick eval

我正在尝试将点击侦听器动态绑定到for循环中的div。

如果第二个jQuery选择器没有+ i,但是经过硬编码,则它可以正常工作,就像这样:

eval(jQuery('#patient_name' + i).on('click touchstart', function(){jQuery('#taskstat0').slideToggle();}));

我还尝试在这样的行中使用两个eval:

eval(jQuery('#patient_name' + i)).on('click touchstart', function(){eval(jQuery('#taskstat' + i)).slideToggle();});

但是这失败了,没有控制台错误:

jQuery(document).ready(function(){
    var patient_count = jQuery('.patient_dropdown').length;
    for(var i=0; i<patient_count; i++){
        eval(jQuery('#patient_name' + i).on('click touchstart', function(){jQuery('#taskstat0').slideToggle();}));
    }
});

我希望每个#patient_namex都在单击时在其下方显示相应的#taskstatx div。

如何使它起作用?

0 个答案:

没有答案