【问题标题】:execute a function after 2 other functions returns a desired response vanilla js在其他 2 个函数返回所需的响应 vanilla js 之后执行一个函数
【发布时间】:2020-04-15 08:52:22
【问题描述】:

我有一个项目,其中包含两个单元格的形式,一个是文本,另一个是日期。 我有 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');
    }
  });
}

我该怎么做?

【问题讨论】:

    标签: javascript execute pure-js


    【解决方案1】:

    您需要从导出的函数中返回布尔值。您不需要每个功能的点击处理程序。最终函数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

    这里是可能对您有所帮助的 sn-p。

    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>

    【讨论】:

    • 如果所有字段都不为空,它可以工作。但是,它对我不起作用。当我按下搜索按钮时,如果文本输入和日期为空,它会记录“失败”。但是如果它们是空的,或者其中一个是空的,它会提醒我这些警报消息之一我将代码分成 3 个文件,正如我在原始帖子中提到的那样,因为它是一个 webpack 项目。我将前两个文件导入到 main.js 文件中
    • --edit -- 如果所有代码都在一个文件中,则您的代码可以正常工作,但如果我将其拆分为 3 个文件。它不会工作
    • 请注意我已经删除了export 关键字。当您将这些函数拆分到不同的文件时将它们添加回来
    • 我将导出并添加事件监听器到每个函数以与用户交互。然后将所有三个函数导出到 index.js >。它工作正常,但总是返回失败。我无法弄清楚问题出在哪里,也无法在评​​论中发布我编辑的代码。所以我把代码放在 code pen 这里是 [link] (codepen.io/mohammedkhairy/project/editor/XvkYVj#0)
    • 请检查此链接以获取工作代码。 codesandbox.io/s/… 你也需要做if (checkDestinationAndDate() &amp;&amp; daysRemaining() ),因为这将进行短路评估,如果日期未填写,daysRemaining 将返回 false
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多