点击提交按钮后,将内容附加到DOM中几秒钟,然后消失。

huangapple go评论65阅读模式
英文:

clicking submit button appends content to the dom for a few seconds then vanishes

问题

I am trying to create a list type todo App. The code works fine up to a point. when I click on my submit button the list item is created and it can be briefly seen on the page after which it disappears. Here is my HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <script src="/script.js" defer></script>
</head>

<body>
   <h1>My shopping list</h1>
   <form class="form-control">
    <div>
      <label for="text">Enter a new item</label>
      <input type="text" name="text" id="123">
    </div>
    <div>
      <button>Submit</button>
    </div>
  </form>
  <ul id="11"></ul>
</body>
</html>

and here is the JavaScript file:

const input = document.querySelector('input')
const list = document.querySelector('ul')
const button = document.querySelector('button')

button.addEventListener('click', () => {
  const myData = input.value
  input.value = '';

  const newdiv = document.createElement('div')
  newdiv.classList.add('newdiv'); 

  const listInput = document.createElement('li')

  const textdata = document.createElement('span')
  textdata.textContent = myData;

  const delButton = document.createElement('button')
  delButton.textContent = 'delete';
  
  listInput.appendChild(textdata)
  newdiv.appendChild(listInput)
  newdiv.appendChild(delButton)

  list.appendChild(newdiv)

  delButton.addEventListener('click', () => {
    list.removeChild(newdiv)
  })
})

希望有人能告诉我我做错了什么。

英文:

I am trying to create a list type todo App. The code works fine up to a point. when I click on my submit button the list item is created and it can be briefly seen on the page after which it disappears. Here is my HTML

&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
    &lt;script src=&quot;/script.js&quot; defer&gt;&lt;/script&gt; 
    
       
&lt;/head&gt;

&lt;body&gt;
   &lt;h1&gt;My shopping list&lt;/h1&gt;
   &lt;form class=&quot;form-control&quot;&gt;
    &lt;div&gt;
      &lt;label for=&quot;text&quot;&gt;Enter a new item&lt;/label&gt;
      &lt;input type=&quot;text&quot; name=&quot;text&quot; id=&quot;123&quot; &gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;button&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  &lt;ul id=&quot;11&quot;&gt;&lt;/ul&gt;
   
&lt;/body&gt;
&lt;/html&gt;

and here is the javacript file

const input = document.querySelector(&#39;input&#39;)
const list = document.querySelector(&#39;ul&#39;)
const button = document.querySelector(&#39;button&#39;)

button.addEventListener(&#39;click&#39;,()=&gt;{
  const myData = input.value
  input.value = &#39;&#39;

  const newdiv = document.createElement(&#39;div&#39;)
  newdiv.classList.add(&#39;newdiv&#39;); 

  const listInput = document.createElement(&#39;li&#39;)

  const textdata = document.createElement(&#39;span&#39;)
  textdata.textContent = myData;

  const delButton = document.createElement(&#39;button&#39;)
  delButton.textContent = &#39;delete&#39;;
  
  listInput.appendChild(textdata)
  newdiv.appendChild(listInput)
  newdiv.appendChild(delButton)

  list.appendChild(newdiv)

  delButton.addEventListener(&#39;click&#39;,()=&gt;{
    list.removeChild(listInput)
  })


})

hopefully someone can tell me what I am doing wrong

答案1

得分: 1

.tags form` the keep and preventDefault see Please.
.https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

}(e) => {
e.preventDefault()

//code...

}```

英文:

Please see preventDefault and keep the form tags.

https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault

button.addEventListener(&#39;click&#39;,(e) =&gt; {
e.preventDefault()

//code...

}


</details>



# 答案2
**得分**: 1

你的代码在没有表单时可以工作,因为表单不会在按钮点击时发送。

正如@Jack所说,要在按钮点击事件中添加event.preventDefault()以防止表单提交。

请查看下面的代码

```html
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = document.querySelector('input')
const list = document.querySelector('ul')
const button = document.querySelector('button')

button.addEventListener('click',(event)=>{
  event.preventDefault();
  const myData = input.value
  input.value = ''

  const newdiv = document.createElement('div')
  newdiv.classList.add('newdiv'); 

  const listInput = document.createElement('li')

  const textdata = document.createElement('span')
  textdata.textContent = myData;

  const delButton = document.createElement('button')
  delButton.textContent = 'delete';
  
  listInput.appendChild(textdata)
  newdiv.appendChild(listInput)
  newdiv.appendChild(delButton)

  list.appendChild(newdiv)

  delButton.addEventListener('click',()=>{
    list.removeChild(listInput)
  })


})

<!-- language: lang-html -->

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <script src="/script.js" defer></script> 
</head>

<body>
   <h1>My shopping list</h1>
   <form class="form-control">
    <div>
      <label for="text">Enter a new item</label>
      <input type="text" name="text" id="123" >
    </div>
    <div>
      <button>Submit</button>
    </div>
  </form>
  <ul id="11"></ul>
   
</body>
</html>

<!-- end snippet -->
英文:

Without form your code works because form is not send on button click.

as @Jack said, add event.preventDefault() to button click event to prevent form submit.

See code below

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = document.querySelector(&#39;input&#39;)
const list = document.querySelector(&#39;ul&#39;)
const button = document.querySelector(&#39;button&#39;)

button.addEventListener(&#39;click&#39;,(event)=&gt;{
  event.preventDefault();
  const myData = input.value
  input.value = &#39;&#39;

  const newdiv = document.createElement(&#39;div&#39;)
  newdiv.classList.add(&#39;newdiv&#39;); 

  const listInput = document.createElement(&#39;li&#39;)

  const textdata = document.createElement(&#39;span&#39;)
  textdata.textContent = myData;

  const delButton = document.createElement(&#39;button&#39;)
  delButton.textContent = &#39;delete&#39;;
  
  listInput.appendChild(textdata)
  newdiv.appendChild(listInput)
  newdiv.appendChild(delButton)

  list.appendChild(newdiv)

  delButton.addEventListener(&#39;click&#39;,()=&gt;{
    list.removeChild(listInput)
  })


})

<!-- language: lang-html -->

&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
    &lt;script src=&quot;/script.js&quot; defer&gt;&lt;/script&gt; 
    
       
&lt;/head&gt;

&lt;body&gt;
   &lt;h1&gt;My shopping list&lt;/h1&gt;
   &lt;form class=&quot;form-control&quot;&gt;
    &lt;div&gt;
      &lt;label for=&quot;text&quot;&gt;Enter a new item&lt;/label&gt;
      &lt;input type=&quot;text&quot; name=&quot;text&quot; id=&quot;123&quot; &gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;button&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  &lt;ul id=&quot;11&quot;&gt;&lt;/ul&gt;
   
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案3

得分: 0

我只是删除了<form></form>标签,它按预期工作。

英文:

I just removed the &lt;form&gt;&lt;/form&gt; tag and it works as expected

huangapple
  • 本文由 发表于 2023年5月26日 15:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定