jQuery的click()闭包循环 [英] Jquery loop of click() closure

查看:199
本文介绍了jQuery的click()闭包循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为print的数组,正在尝试为每个具有打印值ID的按钮获取单独的click事件.与其余的按钮不同,只有我的最后一个按钮似乎可以正常工作,并且通过浏览在线示例,我知道这是一个关闭问题.感谢您的帮助.谢谢

I have an array called print and am trying to get a seperate click event for each button that has id of the print value. Only my last button seems to work unlike the rest and I've looked through online examples and know this is a closure problem. Help is appreciated. Thanks

for(var i = 0; i < print.length; i++){
            var printer = print[i];
            // Watch this:
           $(document).ready(function(){$('#'+printer).click((function(value) {
                return function() {
                     alert(value);
                };
            })(printer));
        });
    }   

推荐答案

$(document).ready(function(){
    for(var i = 0; i < print.length; i++){
            var printer = print[i];
            // Watch this:
           $('#'+printer).click((function(value) {
                return function() {
                     alert(value);
                };
            })(printer));

    }   
})

您的语法很麻烦.您的关闭非常完美,但是文档仅准备一次.

You syntax was screwy. Your closure was perfect, but the document is only ready once.

示例

这篇关于jQuery的click()闭包循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆