【问题标题】:Unable to send html form input to javascript file to console log无法将 html 表单输入发送到 javascript 文件到控制台日志
【发布时间】:2020-07-29 17:59:16
【问题描述】:

我正在尝试从这样的 html 表单中获取输入

<body>
   <div >
      <form>
         <input placeholder="Address" >
         <button >Search</button>
      </form>
   </div>
   <script src="./utils/input.js"></script>
</body>

每当按下提交按钮时,我都会尝试控制台记录表单输入中输入的内容。

utils/input.js 看起来像这样

const address= document.querySelector('form');
const search = document.querySelector('input');


address.addEventListener('submit', (e)=>{
    e.preventDefault();
    const location = search.value;
    console.log(location);
})

但是在 chrome 开发者工具中我得到这样的错误

GET http://127.0.0.1:5500/public/utils/input.js net::ERR_ABORTED 404 (Not Found)

我无法在这里弄清楚我的错误。我做错了什么?

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    input.js 文件未正确导入到您的 html 文件中(因此 not found)。 修复路径,它会工作。

    【讨论】:

    • 嘿,所以我查看了它的实际路径是 ../utils/input.js 但即使这样它也不起作用但是当我将 input.js 转移到与我的 html 代码相同的文件夹时只写 path('input.js') 只有这样它才能工作。你知道为什么会这样吗?
    • @DevangMukherjee 你应该检查相对路径和绝对路径。在不知道文件结构的情况下,我真的不能给你正确的路径。
    【解决方案2】:

    检查 /public/utils/input.js 路径中是否存在 input.js。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 2019-09-20
      • 2018-11-05
      • 2011-11-02
      相关资源
      最近更新 更多