2014-11-07 46 views
3

我试图改变与被点击按钮的innerHTML输入值的价值......我已经尝试了几种方法,但没有工作过变化值的onclick

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

我也试过

<script> 
    function changeValue(){ 
    var cValue = document.getElementbyId('technician').innerhtml; 
    document.getElementbyId('type').value = ('cValue'); 
    } 
</script> 

既不似乎工作

+0

代码尝试的innerText而不是innerHTML的。如果这不起作用,请尝试使用document.getElementbyId('technician')。value。 – dotnetstep 2014-11-07 01:35:22

回答

5

你有几个错字在你的代码 大写和小写字母做事情喜欢的getElementById和innerHTML的

重要

我相信这是你在做什么:

<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" />

+0

我没有看到你所指的错别字......我能够解决这个问题..上面回答了它。你的代码也是如此。其实比我的好。我的代码更加重要。谢谢 – tru 2014-11-07 14:52:26

+0

拼写错误是使用小写字母而不是大写字母。 getElement ** b ** yId与getElement ** B ** yId不同。也内部**的** **是不一样的内** HTML ** – 2014-11-07 21:13:15

+0

这是一个错字在这个问题...对不起..在实际的代码是正确的 – tru 2014-11-07 22:00:35

1

这里是一个非常简单的方法来做到这一点:

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>

我希望这个例子可以帮助!

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

我想这是你想要的东西:

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

感谢您的所有答案。我的问题是,当我打电话了var cVaue我封闭它

document.getElementbyId('type').value = ('cValue'); 

当我应该有这样

document.getElementbyId('type').value = cValue;