将字符串输入到带有按钮的字段?

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

how do I enter a string into a field with a button?

问题

I am new here pls correct me if I do something wrong.
我是新手,请纠正我如果我做错了什么。
I couldn't find a solution for this simple issue, yet.
我还找不到这个简单问题的解决方案。
So I am trying to programm a field where I can enter any text and a button to submit it. When I click on the button the text should be displayed in an other div.
所以我试图编写一个字段,我可以在其中输入任何文本,以及一个提交按钮。当我点击按钮时,文本应该显示在另一个 div 中。

That's what I've programmed so far:
这是我目前已经编写的内容:
Field + Button:
字段 + 按钮:

  1. <form>
  2. <div>
  3. <input name="snfield" type="text" id="snfield" value="any text" maxlength="20" minlength="1" required>
  4. <button onclick="addnewSN('snfield')"><label id="labelsn" for="snfield">Enter Serialnumber</label></button>
  5. </div>
  6. </form>

Script:
脚本:

  1. function addnewSN(text) {
  2. document.getElementById('sn').innerHTML += text;
  3. }

Div for displaying the text:
用于显示文本的 div:

  1. <div id="sn"></div>

When I click on the button the site reloads and the div is empty.
当我点击按钮时,网站重新加载,div 是空的。

英文:

I am new here pls correct me if I do something wrong.
I couldn't find a solution for this simple issue, yet.
So I am trying to programm a field where I can enter any text and a button to submit it. When I click on the button the text should be displayed in an other div.

That's what I've programmed so far:
Field + Button:

  1. &lt;form&gt;
  2. &lt;div&gt;
  3. &lt;input name=&quot;snfield&quot; type=&quot;text&quot; id=&quot;snfield&quot; value=&quot;any text&quot; maxlength=&quot;20&quot; minlength=&quot;1&quot; required&gt;
  4. &lt;button onclick=&quot;addnewSN(&#39;snfield&#39;)&quot;&gt;&lt;label id=&quot;labelsn&quot; for=&quot;snfield&quot;&gt;Enter Serialnumber&lt;/label&gt;&lt;/button&gt;
  5. &lt;/div&gt;
  6. &lt;/form&gt;

Script:

  1. function addnewSN(text) {
  2. document.getElementById(&#39;sn&#39;).innerHTML += text;
  3. }

Div for displaying the text:

  1. &lt;div id=&quot;sn&quot;&gt;&lt;/div&gt;

When I click on the button the site reloads and the div is empty.

答案1

得分: 1

简单的方法是在按钮的onclick代码中返回false,即onclick="addnewSN('snfield'); return false;"。在表单中添加onsubmit="return false;"也可以在这个代码片段中起作用,但不一定总是理想的。

英文:

simplest is to return false from the button onclick code, i.e. onclick=&quot;addnewSN(&#39;snfield&#39;); return false;&quot;
adding onsubmit="return false;" to the form would also work in this snippet as it stands, but may not always be desirable

答案2

得分: 0

  1. 移除 onclick 处理程序
  2. 添加类似以下的处理程序
  1. document.getElementById('target-button').addEventListener('click', function (event) {
  2. event.preventDefault();
  3. // 寻找输入框
  4. // 从中获取值
  5. document.getElementById('sn').innerHTML += 'value';
  6. });
英文:

Or, in other way

  1. Remove onclick handler
  2. Add handler like this
  1. document.getElementById(&#39;target-button&#39;).addEventListener(&#39;click&#39;, function (event) {
  2. event.preventDefault();
  3. // find input
  4. // get value from it
  5. document.getElementById(&#39;sn&#39;).innerHTML += &#39;value&#39;;
  6. });
  7. </details>
  8. # 答案3
  9. **得分**: 0
  10. 你将输入的id传递给函数,而不是输入文本。首先从输入id获取值,然后将其添加到'sn'中。
  11. ```javascript
  12. function addnewSN(id) {
  13. const inputText = document.getElementById(id).value
  14. document.getElementById('sn').innerHTML += inputText;
  15. }
英文:

You are passing input id to the function, not the input text.
First get the value from input id and add it to 'sn'

  1. function addnewSN(id) {
  2. const inputText = document.getElementById(id).value
  3. document.getElementById(&#39;sn&#39;).innerHTML += inputText;
  4. }

huangapple
  • 本文由 发表于 2023年5月17日 16:25:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269993.html
匿名

发表评论

匿名网友

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

确定