价值JavaScript输入

4

我该如何将输入框1中的值传递到输入框2中并添加一些字母?

<script type="text/javascript">

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

</script>

输入1: 2342

输入2: pcid2342d

有人能帮我吗?


你的尝试看起来不错。它不能工作吗? - Gumbo
代码看起来大部分是正确的。你遇到了哪些错误?有什么没有正常工作? - Oded
没有错误,只是我不知道如何在字符串上添加字母 :-) - Sebastian
4个回答

7
只需使用“+”运算符在输入值之前和之后添加字符串即可。
<script type="text/javascript">

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

</script>

1

字符串连接:

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

处理数字时,您可以通过与空字符串连接来避免将数字相加而不是连接到字符串(因为操作符评估顺序的原因):
document.getElementById('input2').value = "" + 1234 + 567 + document.getElementById('input1').value + 89;

0
为什么不尝试一些jQuery呢?
  $("#Anything").click(function() {
     $("#Field1").val($("#Field2").val());
  });

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


对于这样一个微不足道的任务,我认为Jquery过于复杂了! - Tom Gullen

0

看起来您已经完成了工作,现在只需要一个可点击的东西来执行它:

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

我有另一个带有onload事件的输入,所以不需要按钮了,谢谢^^ - Sebastian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接