值 Javascript 输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3158140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:29:27  来源:igfitidea点击:

Value Javascript Input

javascripthtmlinputstring-concatenation

提问by Sebastian

how can i transfer the Value from Input 1 in 2 and add some letters?

如何将输入 1 中的值转移到 2 中并添加一些字母?

<script type="text/javascript">

function doit(){
document.getElementById('input2').value=document.getElementById('input1').value;
}

</script>

Input1: 2342

输入 1:2342

Input2: pcid2342d

输入 2:pcid2342d

Can some one help me?

有人能帮我吗?

回答by greg0ire

Just use the + operator to add a string before and after the input value.

只需使用 + 运算符在输入值前后添加一个字符串。

<script type="text/javascript">

function doit(){
document.getElementById('input2').value="pcid" + document.getElementById('input1').value + "d";
}

</script>

回答by Sebastian

String concatenation:

字符串连接:

document.getElementById('input2').value = "stuff" + document.getElementById('input1').value + "other stuff";

When dealing with numbers you could start by concatenating with empty string to avoid adding numbers together instead of concatenating to strings (because of operator evaluation order):

在处理数字时,您可以先连接空字符串以避免将数字相加而不是连接到字符串(因为运算符评估顺序):

document.getElementById('input2').value = "" + 1234 + 567 + document.getElementById('input1').value + 89;

回答by Kira

Why don't you try something in jQuery?

你为什么不在 jQuery 中尝试一些东西?

  $("#Anything").click(function() {
     $("#Field1").val($("#Field2").val());
  });

The "click" is just a assumption =)

“点击”只是一个假设 =)

回答by Matthew Abbott

Well you seem to have done the work already, all you need is something to click on to execute it:

好吧,您似乎已经完成了这项工作,您只需要单击即可执行它:

<button onclick="doit()">click me</button>