简体   繁体   English

使用for循环访问Jquery内部的数组

[英]Access array inside Jquery using for loop

Hi guys I am trying to loop through 2 arrays , one array handles button Ids , the other handles the text. 嗨,大家好,我正在尝试遍历2个数组,一个数组处理按钮ID,另一个数组处理文本。 However it does not seem to be able to iterate through the text array. 但是,它似乎无法遍历文本数组。 When I try to window.alert the value , it returns undefined. 当我尝试window.alert值时,它返回未定义。

var buttonIdArray = ['#one', '#two']
var textArray = ['this is button one', 'this is button two']

function buttonDetails() {
  for (var i = 0; i < buttonIdArray.length; i++) {
    $(buttonIdArray[i]).click(function() {
      window.alert(textArray[i])
    })
  }
}
<button id ='one'>one</button>
<button id ='two'>two</button>

Because of the different scope in the .click() context you need to get your text from textArray before, like this: 由于textArray .click()上下文中的scope不同,因此您需要先从textArray获取文本,如下所示:

 var buttonIdArray = ['#one', '#two'] var textArray = ['this is button one', 'this is button two'] function buttonDetails() { for (var i = 0; i < buttonIdArray.length; i++) { const text = textArray[i] $(buttonIdArray[i]).click(function() { window.alert(text) }) } } buttonDetails() 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id='one'>one</button> <button id='two'>two</button> 

This is how I would do it. 这就是我要做的。

 var textArray=["this is button 1","this is button 2"];

 $('.myButtons').click(function(){
    var index = $(this).index();
    alert(textArray[index]);
 });


<button class='myButtons' id="one">
Button 1
</button>
<button class='myButtons' id="two">
Button2
</button>

JS fiddle here JS在这里摆弄

Or like this... 或者像这样...

$('.myButtons').click(function(){
  alert($(this).data('text'));
});

<button class='myButtons' id="one" data-text="this is my text">
Button 1
</button>
<button class='myButtons' id="two"  data-text="this is my text 2">
Button2
</button>

Or like this... 或者像这样...

var buttonArray =[
  {id:'#one',text:"this is button1"},
  {id:'#two',text:"this is button2"}
];



for (var i = 0; i < buttonArray.length; i++) {
  var index = i;
  $(buttonArray[index].id).click(function() {
    alert(buttonArray[index].text);
  });
}

Js Fiddle 2 Js小提琴2

Or Like this... 或像这样...

var buttonIdArray = ['#one', '#two']
var textArray = ['this is button one', 'this is button two']

function buttonDetails() {
  for (var i = 0; i < buttonIdArray.length; i++) {
   var text = textArray[i]
   $(buttonIdArray[i]).click(function() {
     window.alert(text)
   })
  }
}

buttonDetails()

You can use index() of jQuery.(like below) 您可以使用jQuery的index() 。(如下所示)

 var buttonIdArray = ['#one','#two']; var textArray=['this is button one','this is button two']; for (var i =0; i<buttonIdArray.length;i++) { $(buttonIdArray[i]).click(function(){ console.log(textArray[$(this).index()-1]); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id ='one'>1</button> <button id ='two'>2</button> 

There is probably a better way to do it than this, but this is how I solved your issue. 可能有比这更好的方法,但这就是我解决您的问题的方式。

var buttonIdArray = ['#one', '#two']
var textArray = ['this is button one', 'this is button two']

function buttonDetails() {
    for (var i = 0; i < buttonIdArray.length; i++) {
        $(buttonIdArray[i]).attr('textArrayIndex', i)
    }
}
$('button').click( function() {
    window.alert(textArray[$(this).attr('textArrayIndex')]);
})
buttonDetails();

Basically, you can't have an event listener within a loop. 基本上,循环内不能有事件侦听器。 It doesn't work. 没用

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM