Javascript 更改输入字段的值 onclick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26792585/
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
change value of input field onclick
提问by tru
I am trying to change the value of an input value with the innerHtml of the button that is clicked ... I have tried a couple of ways but none have worked
我正在尝试使用单击的按钮的 innerHtml 更改输入值的值……我尝试了几种方法,但都没有奏效
<script>
function changeValue(){
var cValue = document.getElementbyId('technician').innerHTML;
var cType = document.getElementbyId('type');
var cType.value = cValue;
}
</script>
<button id="technician" onclick="changeValue()">Technician</button>
<input type="" id="type" name="type" value="change"></input>
I also tried
我也试过
<script>
function changeValue(){
var cValue = document.getElementbyId('technician').innerhtml;
document.getElementbyId('type').value = ('cValue');
}
</script>
neither seems to be working
两者似乎都不起作用
回答by barney.tearspell
you have several typos in your code uppercase and lowercase letters do matter in things like getElementById and innerHTML
你的代码中有几个拼写错误 大写和小写字母在 getElementById 和 innerHTML 之类的东西中很重要
i believe this is what you're trying to do:
我相信这就是你想要做的:
<script>
function changeValue(o){
document.getElementById('type').value=o.innerHTML;
}
</script>
<button id="technician" onclick="changeValue(this)">Technician</button>
<button id="developer" onclick="changeValue(this)">Developer</button>
<input type="text" id="type" name="type" value="change" />
回答by Drazzah
Here's a REALLY simple way to do it:
这是一个非常简单的方法:
function changeValue(value) {
document.getElementById('button1').innerHTML = value;
}
<button onclick="changeValue('This content has changed.')" id="button1">I have some content that will change when you click on me.</button>
I hope that this example helps!
我希望这个例子有帮助!
回答by brunobliss
<button id="technician" onclick="changeValue()">Technician</button>
<input type="" id="type" name="type" value="change"></input>
<script>
function changeValue(){
document.getElementById('type').value="There you go";
}
</script>
回答by bitbyte
I think this is what you want:
我认为这就是你想要的:
<script>
function changeValue(){
document.getElementById('technician').innerHTML = document.getElementById('type').value;
}
</script>
<button id="technician" onclick="changeValue()">Technician</button>
<input id="type" name="type" value="change"></input>
回答by tru
Thanks for all your answers. My problem was, when I called the var cVaue I enclosed it
感谢您的所有回答。我的问题是,当我调用 var cVaue 时,我将其封闭
document.getElementbyId('type').value = ('cValue');
When I should have the code like this
当我应该有这样的代码时
document.getElementbyId('type').value = cValue;

