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

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

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:
字段 + 按钮:

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

Script:
脚本:

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

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

<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:

                            &lt;form&gt;
                                &lt;div&gt;
                                  &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; 
                                  &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;
                                &lt;/div&gt;
                            &lt;/form&gt;

Script:

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

Div for displaying the text:

&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. 添加类似以下的处理程序
document.getElementById('target-button').addEventListener('click', function (event) {
    event.preventDefault();
    // 寻找输入框
    // 从中获取值
    document.getElementById('sn').innerHTML += 'value';
});
英文:

Or, in other way

  1. Remove onclick handler
  2. Add handler like this
document.getElementById(&#39;target-button&#39;).addEventListener(&#39;click&#39;, function (event) {
    event.preventDefault();
    // find input
    // get value from it
    document.getElementById(&#39;sn&#39;).innerHTML += &#39;value&#39;;
});

</details>



# 答案3
**得分**: 0

你将输入的id传递给函数,而不是输入文本。首先从输入id获取值,然后将其添加到'sn'中。

```javascript
function addnewSN(id) {
  const inputText = document.getElementById(id).value
  document.getElementById('sn').innerHTML += inputText;
}
英文:

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

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

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:

确定