Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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将变量从带有循环的方法传递到单击函数_Javascript_Jquery_Scope - Fatal编程技术网

javascript将变量从带有循环的方法传递到单击函数

javascript将变量从带有循环的方法传递到单击函数,javascript,jquery,scope,Javascript,Jquery,Scope,基本上,我试图传递一个在JSON数组中定义的变量,该变量在for循环中解析,以便在click语句中访问 $(document).ready(function() { getJSON(); var usedID; }); function readJSON() { for (var i = 0; i < json.array.length; i ++) { usedID = json.array[i].id; var template = $('repeaterTem

基本上,我试图传递一个在JSON数组中定义的变量,该变量在for循环中解析,以便在click语句中访问

$(document).ready(function() {
  getJSON();
  var usedID;
 });
 function readJSON() {
  for (var i = 0; i < json.array.length; i ++) {
  usedID = json.array[i].id;
  var template = $('repeaterTemplate').clone();
  template.removeAttr('id');
  template.attr('id', 'k' + json.array.id);
  var omega = json.array.id;        
  $('#repeater').append(template);
  (function(o) {
     $('#' + o).click(function() {
    // this is where the submitID click function is now. It passes the omega value in

  });
  })(omega);

 }
 }
 $('.submitID').click(function() {
    submitNow(usedID);
  }

我认为您正在尝试这样做,但是您没有任何方法将特定的用户id与特定的元素相关联

此示例按遇到的顺序将用户id提供给
.submitID
元素。它还假设存在相同数量的用户id和元素

/* this is a non-working example */

var usedIDs = [];

function readJSON() {
    for (var i = 0; i < json.array.length; i++) {
        usedIDs.push(json.array[i].id);
        moreFunctions();
        moreStuff();
    }
}

var j = 0;

$('.submitID').each(function () {

    function (id) {
        $(this).click(function () {
            submitNow(id);
        });
    }(usedIDs[j]);

    j++;

});
/*这是一个不起作用的示例*/
var usedIDs=[];
函数readJSON(){
for(var i=0;i
编辑:根据你的后修订,这是我对你试图做什么的最好猜测

function readJSON() {

    for (var i = 0; i < json.array.length; i++) {

        var usedID = json.array[i].id;

        var template = $('repeaterTemplate').clone();
        template.removeAttr('id');
        template.attr('id', 'k' + usedID);
        $('#repeater').append(template);

        (function (o) {
            // assumign template is a jQuery object here; if not use $(template) instead
            template.click(function () {
                // this is where the submitID click function is now. It passes the omega value in 
                submitNow(o);
            });
        })(usedID);

    }
}
函数readJSON(){ for(var i=0;i
那么您希望每个
.submitID
元素调用
submitNow
,并为
用户ID
指定不同的值?或者它们的值都相同?您可能希望研究使用jQuery的“each”方法对数据进行迭代,这将减少一些循环样板文件。您将在for循环中获得
usedID
值。因此,需要多个ID。你想通过哪一个?-林肯,是的,我需要不同的UserID值。在特定列表上执行单击操作时,将选择正确的值item@sandeepan这是一个自调用的方法-它被定义,然后立即用
omega
作为参数调用。它用于在循环中保存变量的值。嘿,看看我上面的代码示例。我在每个示例中都会遇到一个解析错误function@fordays在
(userID[j])之前缺少
保持冷静!:)
function readJSON() {

    for (var i = 0; i < json.array.length; i++) {

        var usedID = json.array[i].id;

        var template = $('repeaterTemplate').clone();
        template.removeAttr('id');
        template.attr('id', 'k' + usedID);
        $('#repeater').append(template);

        (function (o) {
            // assumign template is a jQuery object here; if not use $(template) instead
            template.click(function () {
                // this is where the submitID click function is now. It passes the omega value in 
                submitNow(o);
            });
        })(usedID);

    }
}