我需要在段落内显示数组中的输入项目,但在提交和显示后项目消失

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

I need to display input items in an array inside paragraph but items disappear after submission and display

问题

以下是翻译好的部分:

"目标是将项目添加到输入框中,并在下面的段落中显示它们。
但在项目显示后,页面会消失。
我需要页面保持原样,不要消失。
下面我包含了在同一HTML文件中的CSS和JavaScript,因为这样更容易复制粘贴。"

请注意,代码部分已被排除在外,只翻译了文本内容。

英文:

The goal is to add items into the input box and display them in the paragraph below it.
But page disappears after the items are displayed.
I need the page to stay the way it is and not disappear.
Bellow I included the css and the javascript inside the same HTML file because it is easier to copy-paste

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

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Page Title&lt;/title&gt;
&lt;style&gt;
h1{
color: Crimson;
}
h3 {
font-family: Arial, Helvetica, sans-serif;
color: gray;
}
#sentence{
width:300px;
height: 200px;
border: 2px dashed darkred;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;Show Room&lt;/h1&gt;

&lt;form onsubmit=&quot;memory()&quot;&gt;
 
 &lt;p&gt;Add items to display bellow&lt;/p&gt;
 
 &lt;textarea id=&quot;box&quot; name=&quot;box&quot; rows=&quot;4&quot; cols=&quot;50&quot; placeholder=&quot;example: frame picture beach ...&quot;&gt;&lt;/textarea&gt;
 
 &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;

&lt;/form&gt;
&lt;h3&gt;Display&lt;/h3&gt;
&lt;p id=&quot;sentence&quot;&gt;&lt;/p&gt;

&lt;script&gt;

var items = [];

function memory() {
  boxvalue = document.getElementById(&#39;box&#39;).value;
  items.push(boxvalue);  
   document.getElementById(&quot;sentence&quot;).innerHTML = items;
  return false;
}

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 2

如果您希望使用memory的返回值来阻止表单提交,您需要在onsubmit属性中明确返回它。

onsubmit="return memory()"

然而,最好使用addEventListenerevent.preventDefault(),而不是内联事件处理程序。

您可以这样实现:

// 首先移除表单上的onsubmit属性
document.querySelector('form').addEventListener('submit', function(e) {
    e.preventDefault();
    memory();
});
英文:

If you want to use the return value from memory to stop the form from submitting, you need to explicitly return it in the onsubmit attribute.

onsubmit=&quot;return memory()&quot;

However, it is better to use addEventListener and event.preventDefault() instead rather than inline event handlers.

You can implement this like so:

// remove the onsubmit attribute on the form first
document.querySelector(&#39;form&#39;).addEventListener(&#39;submit&#39;, function(e) {
    e.preventDefault();
    memory();
});

答案2

得分: 1

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
h1{
color: Crimson;
}
h3 {
font-family: Arial, Helvetica, sans-serif;
color: gray;
}
#sentence{
width:300px;
height: 200px;
border: 2px dashed darkred;
}
</style>
</head>
<body>

<h1>Show Room</h1>

<form onsubmit="memory()">
 
 <p>Add items to display bellow</p>
 
 <textarea id="box" name="box" rows="4" cols="50" placeholder="example: frame picture beach ..."></textarea>
 
 <input type="submit" value="Submit">

</form>
<h3>Display</h3>
<p id="sentence"></p>

<script>

var items = [];

function memory() {
  event.preventDefault();
  boxvalue = document.getElementById('box').value;
  items.push(boxvalue);  
   document.getElementById("sentence").innerHTML = items.toString();
}

</script>
</body>
</html>
英文:

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

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Page Title&lt;/title&gt;
&lt;style&gt;
h1{
color: Crimson;
}
h3 {
font-family: Arial, Helvetica, sans-serif;
color: gray;
}
#sentence{
width:300px;
height: 200px;
border: 2px dashed darkred;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;Show Room&lt;/h1&gt;

&lt;form onsubmit=&quot;memory()&quot;&gt;
 
 &lt;p&gt;Add items to display bellow&lt;/p&gt;
 
 &lt;textarea id=&quot;box&quot; name=&quot;box&quot; rows=&quot;4&quot; cols=&quot;50&quot; placeholder=&quot;example: frame picture beach ...&quot;&gt;&lt;/textarea&gt;
 
 &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;

&lt;/form&gt;
&lt;h3&gt;Display&lt;/h3&gt;
&lt;p id=&quot;sentence&quot;&gt;&lt;/p&gt;

&lt;script&gt;

var items = [];

function memory() {
  event.preventDefault();
  boxvalue = document.getElementById(&#39;box&#39;).value;
  items.push(boxvalue);  
   document.getElementById(&quot;sentence&quot;).innerHTML = items.toString();
}

&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月13日 00:48:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672845.html
匿名

发表评论

匿名网友

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

确定