jQuery“每个”函数值


                    

因此,我具有原始的AJAX功能:</ p>

  dataType:'json',
数据:{动作:某物,ID:ID},
成功:功能(数据){
     var my_title = data.title; //值:“ post1,post2,post3”
     var my_name = data.name; //值:“史蒂夫,麦克,塞恩”
     var my_html ='
'+ my_name +'</ div>'
     jQuery('。my_class')。append(my_html);
}
</ code> </ pre>

因此,我有两个变量( my_titles </ code>和 my_name </ code>),每个变量都有3个值( n </ code>个数字值)。</ p>

然后,我想将这些单独的值用于 my_html </ code>变量,并将它们附加到 .my_class </ code>中。</ p>

结果如下:</ p>

  

    
史蒂夫</ div>
    
迈克</ div>
    
塞恩</ div>
  </ div>
</ code> </ pre>

因此,我尝试如下编辑AJAX函数:</ p>

  dataType:'json',
数据:{动作:某物,ID:ID},
成功:功能(数据){
    jQuery.each(data,function(i,n){
         var my_title = n.title;
         var my_name = n.name;
         alert(“标题:” + my_title);
     });
}
</ code> </ pre>

结果是 undefined </ code>。</ p>

有什么帮助吗? 谢谢!</ p>
     </ div>

展开原文

原文

So, I have the original AJAX function:

dataType: 'json',
data : { action: something , id: id}, 
success: function(data) {
    var my_title= data.title; // values: "post1,post2,post3"      
    var my_name = data.name;  // values: "steve,mike,sean"
    var my_html ='<div class="'+ my_title +'">'+ my_name +'</div>' 
    jQuery('.my_class').append(my_html); 
}

So, I have two variables (my_titles and my_name) each with 3 values (n number values).

Then, I want to use these individual values for my_html variable and append them in the .my_class.

The result will be as follow:

 <div class="my_class">
    <div class="post1">Steve</div>
    <div class="post2">mike</div>
    <div class="post3">sean</div>
 </div>

So, I tried to edit my AJAX function as below:

dataType: 'json',
data : { action: something , id: id}, 
success: function(data) {
   jQuery.each(data,function (i,n){     
        var my_title= n.title;
        var my_name = n.name;               
        alert("Title:" + my_title);             
    });
}

I am getting undefined as the result.

Any help? thanks!

2个回答


假设data.title是一个数组</ p>

  jQuery.each(data.title,function(index){
     var my_title = data.title [index];
     alert(“标题:” + my_title);
});
</ code> </ pre>
     </ div>

展开原文

原文

Assuming data.title is an array

jQuery.each(data.title, function (index){        
    var my_title= data.title[index];               
    alert("Title:" + my_title);             
});


您应该按照以下步骤进行操作。</ p>

  jQuery.each(data.title,function(index){
     var my_title = this; //在此更改this而不是data.title
     alert(“标题:” + my_title);
});
</ code> </ pre>

如果 data.title </ code>是字符串。 您需要 split </ code> data.title </ code>,然后像下面这样遍历它。</ p>

  jQuery.each(data.title.split(','),函数(索引){
     var my_title = this;
     alert(“标题:” + my_title);
});
</ code> </ pre>

更新</ strong>:在成功完成 ajax </ code>之后执行以下操作。</ p>

  var length = data.title.length;
对于(var i = 0; i      var my_title = data.title [i];
     var my_name = data.name [i];
     alert(“标题:” + my_title);
     alert(“ Name:” + my_name);
});
code> </ pre>
     </ div>

展开原文

原文

You should do it like following.

jQuery.each(data.title, function (index){        
    var my_title = this; // change here `this` instead of `data.title`               
    alert("Title:" + my_title);             
});

If data.title is a string. You need to split data.title and then iterate through it like following.

jQuery.each(data.title.split(','), function (index){        
    var my_title= this;               
    alert("Title:" + my_title);             
});

Update: Do like following in ajax success.

var length=data.title.length;
for (var i=0; i<length; i++) {    
    var my_title= data.title[i];
    var my_name = data.name[i];               
    alert("Title:" + my_title); 
    alert("Name:" + my_name);             
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐