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

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

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:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet" href="style.css">
  9. <script src="/script.js" defer></script>
  10. </head>
  11. <body>
  12. <h1>My shopping list</h1>
  13. <form class="form-control">
  14. <div>
  15. <label for="text">Enter a new item</label>
  16. <input type="text" name="text" id="123">
  17. </div>
  18. <div>
  19. <button>Submit</button>
  20. </div>
  21. </form>
  22. <ul id="11"></ul>
  23. </body>
  24. </html>

and here is the JavaScript file:

  1. const input = document.querySelector('input')
  2. const list = document.querySelector('ul')
  3. const button = document.querySelector('button')
  4. button.addEventListener('click', () => {
  5. const myData = input.value
  6. input.value = '';
  7. const newdiv = document.createElement('div')
  8. newdiv.classList.add('newdiv');
  9. const listInput = document.createElement('li')
  10. const textdata = document.createElement('span')
  11. textdata.textContent = myData;
  12. const delButton = document.createElement('button')
  13. delButton.textContent = 'delete';
  14. listInput.appendChild(textdata)
  15. newdiv.appendChild(listInput)
  16. newdiv.appendChild(delButton)
  17. list.appendChild(newdiv)
  18. delButton.addEventListener('click', () => {
  19. list.removeChild(newdiv)
  20. })
  21. })

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

英文:

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

  1. &lt;html lang=&quot;en&quot;&gt;
  2. &lt;head&gt;
  3. &lt;meta charset=&quot;UTF-8&quot;&gt;
  4. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;title&gt;Document&lt;/title&gt;
  7. &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  8. &lt;script src=&quot;/script.js&quot; defer&gt;&lt;/script&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;h1&gt;My shopping list&lt;/h1&gt;
  12. &lt;form class=&quot;form-control&quot;&gt;
  13. &lt;div&gt;
  14. &lt;label for=&quot;text&quot;&gt;Enter a new item&lt;/label&gt;
  15. &lt;input type=&quot;text&quot; name=&quot;text&quot; id=&quot;123&quot; &gt;
  16. &lt;/div&gt;
  17. &lt;div&gt;
  18. &lt;button&gt;Submit&lt;/button&gt;
  19. &lt;/div&gt;
  20. &lt;/form&gt;
  21. &lt;ul id=&quot;11&quot;&gt;&lt;/ul&gt;
  22. &lt;/body&gt;
  23. &lt;/html&gt;

and here is the javacript file

  1. const input = document.querySelector(&#39;input&#39;)
  2. const list = document.querySelector(&#39;ul&#39;)
  3. const button = document.querySelector(&#39;button&#39;)
  4. button.addEventListener(&#39;click&#39;,()=&gt;{
  5. const myData = input.value
  6. input.value = &#39;&#39;
  7. const newdiv = document.createElement(&#39;div&#39;)
  8. newdiv.classList.add(&#39;newdiv&#39;);
  9. const listInput = document.createElement(&#39;li&#39;)
  10. const textdata = document.createElement(&#39;span&#39;)
  11. textdata.textContent = myData;
  12. const delButton = document.createElement(&#39;button&#39;)
  13. delButton.textContent = &#39;delete&#39;;
  14. listInput.appendChild(textdata)
  15. newdiv.appendChild(listInput)
  16. newdiv.appendChild(delButton)
  17. list.appendChild(newdiv)
  18. delButton.addEventListener(&#39;click&#39;,()=&gt;{
  19. list.removeChild(listInput)
  20. })
  21. })

hopefully someone can tell me what I am doing wrong

答案1

得分: 1

  1. .tags form` the keep and preventDefault see Please.
  2. .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

  1. button.addEventListener(&#39;click&#39;,(e) =&gt; {
  2. e.preventDefault()
  3. //code...
  4. }
  5. </details>
  6. # 答案2
  7. **得分**: 1
  8. 你的代码在没有表单时可以工作,因为表单不会在按钮点击时发送。
  9. 正如@Jack所说,要在按钮点击事件中添加event.preventDefault()以防止表单提交。
  10. 请查看下面的代码
  11. ```html
  12. <!-- begin snippet: js hide: false console: true babel: false -->
  13. <!-- language: lang-js -->
  14. const input = document.querySelector('input')
  15. const list = document.querySelector('ul')
  16. const button = document.querySelector('button')
  17. button.addEventListener('click',(event)=>{
  18. event.preventDefault();
  19. const myData = input.value
  20. input.value = ''
  21. const newdiv = document.createElement('div')
  22. newdiv.classList.add('newdiv');
  23. const listInput = document.createElement('li')
  24. const textdata = document.createElement('span')
  25. textdata.textContent = myData;
  26. const delButton = document.createElement('button')
  27. delButton.textContent = 'delete';
  28. listInput.appendChild(textdata)
  29. newdiv.appendChild(listInput)
  30. newdiv.appendChild(delButton)
  31. list.appendChild(newdiv)
  32. delButton.addEventListener('click',()=>{
  33. list.removeChild(listInput)
  34. })
  35. })
  36. <!-- language: lang-html -->
  37. <html lang="en">
  38. <head>
  39. <meta charset="UTF-8">
  40. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  41. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  42. <title>Document</title>
  43. <link rel="stylesheet" href="style.css">
  44. <script src="/script.js" defer></script>
  45. </head>
  46. <body>
  47. <h1>My shopping list</h1>
  48. <form class="form-control">
  49. <div>
  50. <label for="text">Enter a new item</label>
  51. <input type="text" name="text" id="123" >
  52. </div>
  53. <div>
  54. <button>Submit</button>
  55. </div>
  56. </form>
  57. <ul id="11"></ul>
  58. </body>
  59. </html>
  60. <!-- 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 -->

  1. const input = document.querySelector(&#39;input&#39;)
  2. const list = document.querySelector(&#39;ul&#39;)
  3. const button = document.querySelector(&#39;button&#39;)
  4. button.addEventListener(&#39;click&#39;,(event)=&gt;{
  5. event.preventDefault();
  6. const myData = input.value
  7. input.value = &#39;&#39;
  8. const newdiv = document.createElement(&#39;div&#39;)
  9. newdiv.classList.add(&#39;newdiv&#39;);
  10. const listInput = document.createElement(&#39;li&#39;)
  11. const textdata = document.createElement(&#39;span&#39;)
  12. textdata.textContent = myData;
  13. const delButton = document.createElement(&#39;button&#39;)
  14. delButton.textContent = &#39;delete&#39;;
  15. listInput.appendChild(textdata)
  16. newdiv.appendChild(listInput)
  17. newdiv.appendChild(delButton)
  18. list.appendChild(newdiv)
  19. delButton.addEventListener(&#39;click&#39;,()=&gt;{
  20. list.removeChild(listInput)
  21. })
  22. })

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

  1. &lt;html lang=&quot;en&quot;&gt;
  2. &lt;head&gt;
  3. &lt;meta charset=&quot;UTF-8&quot;&gt;
  4. &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
  5. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  6. &lt;title&gt;Document&lt;/title&gt;
  7. &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
  8. &lt;script src=&quot;/script.js&quot; defer&gt;&lt;/script&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;h1&gt;My shopping list&lt;/h1&gt;
  12. &lt;form class=&quot;form-control&quot;&gt;
  13. &lt;div&gt;
  14. &lt;label for=&quot;text&quot;&gt;Enter a new item&lt;/label&gt;
  15. &lt;input type=&quot;text&quot; name=&quot;text&quot; id=&quot;123&quot; &gt;
  16. &lt;/div&gt;
  17. &lt;div&gt;
  18. &lt;button&gt;Submit&lt;/button&gt;
  19. &lt;/div&gt;
  20. &lt;/form&gt;
  21. &lt;ul id=&quot;11&quot;&gt;&lt;/ul&gt;
  22. &lt;/body&gt;
  23. &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:

确定