jQuery在for循环变量范围内绑定 [英] jQuery bind inside a for loop variable scope

查看:107
本文介绍了jQuery在for循环变量范围内绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次了,但是我无法从堆栈O上找到的问题解决.这是我的代码

I know this question has been asked many times but I haven't been able to resolve from what I found on Stack O. Here is my code

for(var i=0; i<retrievedSearchesListLength; i++){
retrievedSearchesListProv = retrievedSearchesList[i].searchId;
retrievedSearchesListType = retrievedSearchesList[i].searchParameters;

    function getEventHandlerFunction(a){
    $J.cookies.set('ps_clickedsearch',a);
}

 $J('#submitSearch'+i).bind('click',getEventHandlerFunction(retrievedSearchesListType));
}


每次生成的值都是最后一个for循环值.如何保持范围,以便单击链接可以得到正确的值?


Everytime the resulting value is the last for loop value. How do I keep scope so that the link clicked results in the correct value?

我需要正确的retriatedSearchesListType来反映单击链接时的情况.

I need the correct retrievedSearchesListType to reflect when the link is clicked.

预先感谢

推荐答案

您需要从getEventHandlerFunction

for(var i=0; i<retrievedSearchesListLength; i++){
    retrievedSearchesListProv = retrievedSearchesList[i].searchId;
    retrievedSearchesListType = retrievedSearchesList[i].searchParameters;

    function getEventHandlerFunction(a){
        return function(){
            $J.cookies.set('ps_clickedsearch',a);
        }
    }

    $J('#submitSearch'+i).bind('click',getEventHandlerFunction(retrievedSearchesListType));
}

这篇关于jQuery在for循环变量范围内绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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