【问题标题】:Creating button that axios.post something from input从输入中创建 axios.post 的按钮
【发布时间】:2021-03-02 10:18:59
【问题描述】:

如果输入中有内容,我必须创建一个按钮来执行 axios.post 请求。 我卡住了,我的 HTML:

<!DOCTYPE html>
<html lang="pl-PL">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="HandheldFriendly" content="true">
    <title>zad2</title>
  </head>
  <body>
    <input id="newInput" data-test="todo-input" placeholder="wpisz tekst"><button id="newButton" data-test="todo-add" onClick="addToDoElem()">Dodaj</button>
  </body>
</html>

还有我的 js:

const $newTask;
let $newInput = document.getElementById('.newInput');

let $newButton;
function addToDoElem() {
  if ($newInput.value !== '') {
    axios.post(
      $newTask = $newInput.value;
    )
  }
}

感谢任何提示。祝你有美好的一天!

【问题讨论】:

  • document.querySelector('.newInput') 不会在此处选择任何内容。您的元素没有 class newInput.

标签: javascript function button input axios


【解决方案1】:

只需将document.getElementById('.newInput'); 更改为document.getElementById('#newInput');getElementById() 通过他的 id 而不是他的类获取 DOM 元素。

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 2020-10-21
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多