0

我有一个项目,其中包含两个单元格的形式,一个是文本,另一个是日期。如果文本字段和日期字段具有正确的输入来执行新功能,我有 2 个 JS 文件可以验证我想要的每个单元格。如果不显示我在其他 JS 文件中编码的警报消息,我尝试了很多,但我不知道是什么问题?

第一个 JS 文件

export function daysRemaining() {
  document.getElementById('search').addEventListener('click', (event) => {
    event.preventDefault();
    //console.log("I'm there from search form");
    const today = new Date(); //today date
    const tripDate = new Date(document.getElementById('date').value); //trip date entered by user
    if (tripDate > today) {
      console.log(
        Math.ceil(Math.abs(tripDate - today) / (1000 * 60 * 60 * 24))
      );
    }
  });
}

第二个文件

export function checkDestinationAndDate() {
  document.getElementById('search').addEventListener('click', (event) => {
    event.preventDefault();
    const destination = document.querySelector('#destination').value;
    const tripDate = new Date(document.getElementById('date').value);
    if (!destination && !Date.parse(tripDate)) {
      alert('Enter a destination and a date');
    } else if (destination && !Date.parse(tripDate)) {
      alert("You didn't choose a date");
    } else if (!destination && Date.parse(tripDate)) {
      alert("You didn't choose a destination");
    } else if (destination && Date.parse(tripDate) < new Date()) {
      alert('Please pick a future date');
    } else {
      console.log(destination);
    }
  });
}

*第三个JS文件和我要编辑的内容*

import { daysRemaining } from './days_countdown';
import { checkDestinationAndDate } from './destination_date';

export function main() {

  document.getElementById('search').addEventListener('click', (event) => {
    event.preventDefault();

    if (daysRemaining === true && checkDestinationAndDate === true) {
      console.log('good work');
    } else {
      console.log('Failed');
    }
  });
}

我怎样才能做到这一点?

4

1 回答 1

0

您需要从导出的函数中返回布尔值。您不需要每个功能的点击处理程序。最后一个函数main()可以监听点击事件。

export编辑:如果分成模块,您需要发挥作用。请检查代码和框链接。

https://codesandbox.io/s/stackoverflow-answer-execute-a-function-after-2-other-functions-returns-a-desired-response-vanilla-js-rcze7?file=/src/index.js

这是可能对您有所帮助的片段。

function daysRemaining() {
  //console.log("I'm there from search form");
  const today = new Date(); //today date
  const tripDate = new Date(document.getElementById('date').value); //trip date entered by user
  if (tripDate > today) {
    const isRemaining = Math.ceil(Math.abs(tripDate - today) / (1000 * 60 * 60 * 24))
    console.log(isRemaining);

    return !!isRemaining
  }

  return false;
}

function checkDestinationAndDate() {
  const destination = document.querySelector('#destination').value;
  const tripDate = new Date(document.getElementById('date').value);
  let returnValue = false;

  if (!destination && !Date.parse(tripDate)) {
    alert('Enter a destination and a date');
  } else if (destination && !Date.parse(tripDate)) {
    alert("You didn't choose a date");
  } else if (!destination && Date.parse(tripDate)) {
    alert("You didn't choose a destination");
  } else if (destination && Date.parse(tripDate) < new Date()) {
    alert('Please pick a future date');
  } else {
    returnValue = true;
    console.log(destination);
  }

  return returnValue;
}

// import { daysRemaining } from './days_countdown';
// import { checkDestinationAndDate } from './destination_date';

function main() {

  document.getElementById('search').addEventListener('click', (event) => {
    event.preventDefault();

    if (checkDestinationAndDate() && daysRemaining()) {
      console.log('good work');
    } else {
      console.log('Failed');
    }
  });
}

main()
<input type="date" id="date" />
<input type="text" id="destination" />

<button type="button" id="search">Search</button>

于 2020-04-15T09:03:35.513 回答