Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 绑定多个副本时,Jquery click绑定无法正常工作_Javascript_Jquery_Cloning - Fatal编程技术网

Javascript 绑定多个副本时,Jquery click绑定无法正常工作

Javascript 绑定多个副本时,Jquery click绑定无法正常工作,javascript,jquery,cloning,Javascript,Jquery,Cloning,在创建模板副本并将.click()方法正确绑定到它们时,我似乎遇到了一个问题。以以下javascript为例: function TestMethod() { var test = Array(); test[0] = 0; test[1] = 1; test[2] = 2; // Insert link into the page $("#test_div").html("<a href=\"#\"></a><br

在创建模板副本并将.click()方法正确绑定到它们时,我似乎遇到了一个问题。以以下javascript为例:

function TestMethod() {
    var test = Array();
    test[0] = 0;
    test[1] = 1;
    test[2] = 2;

    // Insert link into the page
    $("#test_div").html("<a href=\"#\"></a><br>");
    var list;
    for (x = 0; x < test.length; x++) {
        var temp = $("#test_div").clone();
        temp.find('a').html("Item #" + test[x]);
        temp.click(function () { alert(x); });

        if (list == undefined)
            list = temp;
        else
            list = list.append(temp.contents());
    }
    $("#test_div2").append(list);
 }
函数TestMethod(){
var test=Array();
测试[0]=0;
试验[1]=1;
试验[2]=2;
//在页面中插入链接
$(“#test_div”).html(“
”); var列表; 对于(x=0;x
我看到的问题是,无论用户单击哪个项目,它都会运行警报(2),即使在您单击前几个项目时也是如此

我怎样才能让它工作


编辑:我做了一个非常简单的例子,应该可以更清楚地说明这个问题。无论您单击哪个项目,它都会显示一个带有数字2的警告框

如果我错了,请纠正我,
.valueOf()
在JS中返回布尔对象的基本值

这不会发生
ShowObject(5,'T')<代码>显示对象(objectVal.valueOf(),'T')

为什么不直接使用
对象[x].Value
<代码>显示对象(对象[x]。值“T”)

哇哦

经过深入研究。。。我找到了一个解决办法

因为这是一个结束,它不会真的那样工作…
这是一个解决方案

temp.find('a').bind('click', {testVal: x},function (e) { 
   alert(e.data.testVal);
   return false;
});

为了得到最好的解释。。。在页面的中间部分,它说“强>传递事件数据>BR>

>P>我认为你的问题源于对JavaScript中的范围的误解。(如果我错了,我道歉。)

或者,使用基于回调的函数,例如:


对于(x=0;x
,什么是
objects
?修复了它。它是var objects=data.objects;(数据是$.ajax传递的json结构)我得到了另一个解决方案…请再次阅读我的答案…:)干杯!两个解决方案都有效,但我选择将Reigel标记为答案,只是因为我选择不使用Jquery.Each()并只使用bind()方法。我尝试了objectVal,objectVal.ValueOf()方法,等等。这一切似乎产生了相同的结果,所有克隆的对象都只使用了最后一个objectVal值。对象[x]之间是否存在差异。代码中的值
和对象[x]。文本?是的,值返回我的Id号,文本返回对象的名称。两者都被正确读取(在Chrome的调试器中测试)啊,我明白了,我知道必须有更好的方法来传递值,我只是不知道我可以这样做!好的,这很有意义!我下班回家后会试试,谢谢!
function () {
    for (...) {
        var foo = ...;

        $('<div>').click(function () { alert(foo); }).appendTo(...);
    }
}
function () {
    for (...) {
        (function (foo) {
            $('<div>').click(function () { alert(foo); }).appendTo(...);
        })(...);
    }
}
function () {
    $.each(..., function (i, foo) {
        $('<div>').click(function () { alert(foo); }).appendTo(...);
    });
}
var list;
jQuery.each(data.objects, function (x, object) {

    // Clone the object list item template
    var item = $("#object_item_list_template").clone();

    // Setup the click action and inner text for the link tag in the template
    var objectVal = object.Value;
    item.find('a').click(function () { ShowObject(objectVal.valueOf(), 'T'); }).html(object.Text);

    // add the html to the list
    if (list == undefined)
        list = item;
    else
        list.append(item.contents());
});