简体   繁体   English

在 javascript 中检索多个 html 值

[英]retrieve multiple html values in javascript

On my node.js backend, I have this:在我的node.js后端,我有这个:

  {
    DATE: '2021-12-11 02:17:59.317432',
    LOCATION: 'Location',
    ASSIGNED_TRAINER: 'john.smith7@g.com',
    CLIENT_USERNAME: 'Test User Name'
  },
  {
    DATE: '2022-01-04 02:27:11.278146',
    LOCATION: 'Location',
    ASSIGNED_TRAINER: 'john.smith7@g.com',
    CLIENT_USERNAME: 'Test User Name'
  },
  {
    DATE: '2021-12-15 10:30:00.000000',
    LOCATION: 'Location',
    ASSIGNED_TRAINER: 'john.smith7@g.com',
    CLIENT_USERNAME: 'Test User Name'
  }

and on the front-end, I am trying to grab the DATE field.在前端,我正在尝试获取DATE字段。 and so what I do is this:所以我要做的是:

 <% if (data.length) {
    for(var i = 0; i < data.length; i++) { %>
      <input type="text" name="passedDates" id="passedDates" value="<%= data[i].DATE %>">
      <%  }
    } else { %>
   <% } %>

and then in my <script> tag, what I do is:然后在我的<script>标签中,我所做的是:

  for ( var z = 1; z <= lastDayOfM; z++ ) {
    
    const yourDate = new Date()
    var theDate = yourDate.toISOString().split('T')[0]

*here I am trying to get the values that I pass through the id 'passedDates' *在这里,我试图获取通过 id 'passedDates' 传递的值

    var passedDates = document.getElementById("passedDates").value

    var finalDate = passedDates.split('-');

    var yyyy = finalDate[0];
    var mm = finalDate[1];
    var dd = finalDate[2];
    var theFinalDay = dd.split(' ')

but then I log yyyy , mm , dd , and then theFinalDay[0] .但后来我记录了yyyymmdd ,然后theFinalDay[0] But it doesn't store ALL of the date values in there.但它不会在其中存储所有日期值。 I need all of them in there, because then when going below:我需要所有这些,因为然后在下面时:

    // check if todays date
    if ( z == today.getDate() && y == today.getFullYear() && m == today.getMonth() ) {
      var myTestPassedDate = theFinalDay[0]
      if (myTestPassedDate) {

*here i try to insert something into HTML , but it won't do all 3 because it only stores one. *在这里我尝试在HTML中插入一些东西,但它不会全部完成 3 个,因为它只存储一个。

 day.innerHTML = myTestPassedDate + "<br><img src='https://miro.medium.com/max/512/1*nZ9VwHTLxAfNCuCjYAkajg.png' style='height: 10px; width: 10px;'>"
 

 }
      day.classList.add("today");
    }

    

how do I fix what I am trying to do?我该如何解决我想要做的事情? sorry if it is confusing, feel free to ask questions.很抱歉,如果它令人困惑,请随时提出问题。

To grab all dates from your inputs you could do:要从您的输入中获取所有日期,您可以执行以下操作:

  1. Add a class to your input fields, eg js-date将 class 添加到您的输入字段,例如js-date

  2. Select all the inputs and grab their values Select 所有输入并获取它们的值

const getDatesFromInputs = (className) => {
  const inputNodes = document.querySelectorAll(className); // get us a NodeList
  const inputArray = Array.from(inputNodes); // turn NodeList into Array to access .map() function

  const dates = inputArray.map((inputEl) => new Date(inputEl.value));
  return dates // return Array of dates
};
  1. Do with the dates whatever you like (I did not understand what you want to do with them)随心所欲地处理日期(我不明白你想用它们做什么)
const datesFromInput = getDatesFromInputs('.js-date');

const today = new Date();
const y = today.getFullYear();
const m = today.getMonth();
const d = today.getDate();

const isTodayInDates = datesFromInput.some((date) => date.getFullYear() === y && date.getMonth() === m && date.getDate() === d);

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

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