简体   繁体   English

如何遍历一个jQuery数组,然后一次输出一项(从索引0开始)?

[英]How can I loop through a jquery array and then output each item (starting at index 0) one at a time?

I'm trying to collect user responses and add them into the answers array. 我正在尝试收集用户响应并将其添加到答案数组。 Then I want to display the most recent user input ( answers[0] ) into the .user-answer div . 然后,我想在.user-answer div显示最新的用户输入( .user-answer div answers[0] )。 I've managed to get that part taken care of but if you see a better way to do it then please show me. 我已经设法解决了这一部分,但是如果您发现更好的方法,请告诉我。

The second part of is that I want to show the items in the array one at a time in the .dynamic-content h2 slot. 第二部分是我想一次在.dynamic-content h2插槽中显示一个数组中的项。 I need to loop through the array (starting at answers[0]), pull out each item, show it in the div and then move to the next item and show it in the div . 我需要遍历数组(从answers [0]开始),拉出每个项目,将其显示在div中,然后移至下一个项目,并将其显示在div

Here's a link to the CodePen . 这是CodePen的链接。

HTML HTML

<div class="answer">
  <h1>Life, Liberty, and </h1>
</div>

<div class="user-answer">
  <h1>_________</h1>
</div>

<input type="text"/>
<input type="submit"/>

<div class="dynamic-content">
  <h1>What is your pursuit of happiness?</h1>
  <h2>Output array items here</h2>
</div>

JavaScript 的JavaScript

// create an empty array
var answers = []; 

// STORE AND OUTPUT DATA ON SUBMISSION

function handleUserInput() {

  // store user input
  var userInput = $('input[type=text]').val(); 

  // append input value to answers array
  answers.unshift(userInput); 

  // add latest user input into the HTML
  $('.user-answer').html('<h1>' + answers[0] + '</h1>'); 

}

// RUN FUNCTION ON SUBMISSION

$('input[type=submit]').on('click', function() {
  handleUserInput();
});

First, I don't know if you want to do this on server side or in client side, but for server side you need to make it work with a server side scripting language, like PHP , or Perl . 首先,我不知道您是要在服务器端还是在客户端执行此操作,但是对于服务器端,您需要使其与服务器端脚本语言(例如PHPPerl)一起使用 For clent side, you need to cancel the default submit event when user clicks the submit button, else the page will refresh posting the form data. 从侧面看,您需要在用户单击“提交”按钮时取消默认的提交事件,否则页面将刷新以发布表单数据。

So, to do this without the page refreshing, first add the event to the onclick event and pass it to your handleUserInput function like this: 因此,要在不刷新页面的情况下执行此操作,请首先将事件添加到onclick事件,然后将其传递给您的handleUserInput函数,如下所示:

$('input[type=submit]').on('click', function(e) {
  handleUserInput(e);
  rotate();
});

then, cancel the event by using preventDefault to the event object: 然后,通过对事件对象使用preventDefault来取消事件:

e.preventDefault();

now, to display the data to .dynamic-content and add the answers in h2 tags, you first need to remove all h2 elements (because you already have an h2 element there, or you could also prepend if you remove the h2 Output array items here tag) and then add all the answers starting from the first one like this: 现在,以显示数据.dynamic-content并添加答案h2标签,你首先需要删除所有的h2元素(因为你已经有一个h2元素存在,或者你也可以prepend如果删除了h2 Output array items here标记),然后从第一个开始添加所有答案,如下所示:

$('.dynamic-content h2').remove();

$.each(answers, function(i, v) {
  $('.dynamic-content').append($('<h2/>').text(v));
});

The final code will be something like this: 最终代码将如下所示:

var answers = []; // create an empty array

// STORE DATA ON SUBMISSION
function handleUserInput(e) {
  e.preventDefault();

  var userInput = $('input[type=text]').val(); // store user input

  answers.unshift(userInput); // append value to answers array

  // $('.user-answer').fadeIn().html('<h1>' + answers[0] + '</h1>').delay( 500 ).fadeOut(); // add user input into the HTML

  $('.user-answer').html('<h1>' + answers[0] + '</h1>'); // add user input into the HTML

  $('.dynamic-content h2').remove();

  $.each(answers, function(i, v) {
    $('.dynamic-content').append($('<h2/>').text(v));
  });

  // $('.answer').html('<h1>' + answers[0] + '</h1>'); 
  // $.each(answers[0 + i], function() {
  //   $('.answer').fadeIn().html('<h1>' + answers + '</h1>').delay( 500 ).fadeOut();
  // });

}

$('input[type=submit]').on('click', function(e) {
  handleUserInput(e);
  rotate();
});

http://codepen.io/clytras/pen/zoBXpE http://codepen.io/clytras/pen/zoBXpE

Just change your JS a little bit to this: 只需将您的JS更改为:

  var answers = []; // create an empty array // STORE DATA ON SUBMISSION function handleUserInput() { var userInput = $('input[type=text]').val(); $('.user-answer').html('<h1>' + userInput + '</h1>'); answers.push(userInput); $('.dynamic-content h2').html(answers + ', '); } $('input[type=submit]').on('click', function() { handleUserInput(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="answer"> <h1>Life, Liberty, and </h1> </div> <div class="user-answer"> <h1>_________</h1> </div> <input type="text"/> <input type="submit"/> <div class="dynamic-content"> <h1>What is your pursuit of happiness?</h1> <h2>Output array items here</h2> </div> 

It's not the best way but here it goes. 这不是最好的方法,但是可以解决。 This method is like you asked to change the SAME DIV dynamically, so no other items are created, they just "change" 就像您要求动态更改SAME DIV一样,因此没有其他项被创建,它们只是“更改”

Add this function: 添加此功能:

function rotateTerm() {
  if(answers.length>0){
  var ct = $("#rotate").data("term") || 0;
  $("#rotate").data("term", ct == answers.length -1 ? 0 : ct + 1).text(answers[ct]).fadeIn().delay(2000).fadeOut(200,function(){
rotateTerm();
  });
 }
}
$(rotateTerm);

Then in your submission put: 然后在您的提交中输入:

$('input[type=submit]').on('click', function() {
  handleUserInput(); 
  $(rotateTerm);
});

working CodePen thanks to Nick Craver's answer in this thread . 感谢Nick Craver在此主题中的回答,使CodePen可以正常工作。

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

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