如何在 JavaScript(而非 JQuery)的文本字段中显示按钮值
How do i display button value in a text field in JavaScript(not JQuery)
我做了一个简单的计算器,但我无法在单击文本字段时打印按钮的值。我怎么做?我只想使用 JavaScript,而不是 JQuery。当我单击我的按钮时,值未显示在文本字段 "scr" 上。我该如何实现?
这是我的代码:
<html>
<head>
<title>Calculator</title>
<style>
</style>
<script type="javascript">
function display0()
{
a=document.all.f1.zero.value;
document.all.getElementById("scr").innerHTML=a;
b=document.all.f1.one.value;
document.all.getElementById('scr').value=b;
c=document.all.f1.two.value;
document.all.getElementById("scr").innerHTML=c;
d=document.all.f1.three.value;
document.all.getElementById("scr").innerHTML=d;
e=document.all.f1.four.value;
document.all.getElementById("scr").innerHTML=e;
f=document.all.f1.five.value;
document.all.getElementById("scr").innerHTML=f;
g=document.all.f1.six.value;
document.all.getElementById("scr").innerHTML=g;
h=document.all.f1.seven.value;
document.all.getElementById("scr").innerHTML=h;
i=document.all.f1.eight.value;
document.all.getElementById("scr").innerHTML=i;
j=document.all.f1.nine.value;
document.all.getElementById("scr").innerHTML=j;
k=document.all.f1.sum.value;
document.all.getElementById("scr").innerHTML=k;
l=document.all.f1.sub;
document.all.getElementById("scr").innerHTML=l;
m=document.all.f1.mul;
document.all.getElementById("scr").innerHTML=m;
n=document.all.f1.div;
document.all.getElementById("scr").innerHTML=n;
o=document.all.f1.clear;
document.all.getElementById("scr").innerHTML=o;
}
</script>
</head>
<body>
<form name="f1" id="f1" method="post">
<div>
<input type="text" id="scr" name="scr" />
</div>
<br/>
<div>
<input type="button" value="7" onclick="display7()" id="seven"name="seven"/>
<input type="button" value="8" onclick="display8()" id="eight"name="eight"/>
<input type="button" value="9" onclick="display9()" id="nine"name="nine"/>
<input type="button" value="/" onclick="display/()" id="div"name="div"/>
<br/>
<input type="button" value="4" onclick="display4()" id="four"name="four"/>
<input type="button" value="5" onclick="display5()" id="five"name="five"/>
<input type="button" value="6" onclick="display6()" id="six"name="six"/>
<input type="button" value="X" onclick="displayX()" id="mul"name="mul"/>
<br/>
<input type="button" value="1" onclick="display()" id="one" name="one" />
<input type="button" value="2" onclick="display2()" id="two"name="two"/>
<input type="button" value="3" onclick="display3()" id="three"name="three"/>
<input type="button" value="-" onclick="display-()" id="sub"name="sub"/>
<br/>
<input type="button" value="CLR" onclick="displayC()"/>
<input type="button" value="0" onclick="display0()" id="zero"name="zero"/>
<input type="button" value="=" onclick="display=()" id="eq"name="eq"/>
<input type="button" value="+" onclick="display+()" id="sum"name="sum"/>
</div>
<br/>
</form>
</body>
</html>
你可以试试这个
HTML
<button id="plus">Plus</button>
<input type="text" id="ih"></input>
Javascript
document.getElementById("ih").value=document.getElementById("plus").innerHTML;
试试这个:
var showValue = function(val){
document.getElementById('pressed').value = parseInt(val);
}
<p><input type="text" id="pressed" value="" /></p>
<p>
<button onclick="showValue(1)">1</button>
<button onclick="showValue(2)">2</button>
<button onclick="showValue(3)">3</button>
</p>
<input type="button" value="test" onClick="document.getElementById('textfield').innerHTML=this.value">
<div id="textfield"></div>
我做了一个简单的计算器,但我无法在单击文本字段时打印按钮的值。我怎么做?我只想使用 JavaScript,而不是 JQuery。当我单击我的按钮时,值未显示在文本字段 "scr" 上。我该如何实现?
这是我的代码:
<html>
<head>
<title>Calculator</title>
<style>
</style>
<script type="javascript">
function display0()
{
a=document.all.f1.zero.value;
document.all.getElementById("scr").innerHTML=a;
b=document.all.f1.one.value;
document.all.getElementById('scr').value=b;
c=document.all.f1.two.value;
document.all.getElementById("scr").innerHTML=c;
d=document.all.f1.three.value;
document.all.getElementById("scr").innerHTML=d;
e=document.all.f1.four.value;
document.all.getElementById("scr").innerHTML=e;
f=document.all.f1.five.value;
document.all.getElementById("scr").innerHTML=f;
g=document.all.f1.six.value;
document.all.getElementById("scr").innerHTML=g;
h=document.all.f1.seven.value;
document.all.getElementById("scr").innerHTML=h;
i=document.all.f1.eight.value;
document.all.getElementById("scr").innerHTML=i;
j=document.all.f1.nine.value;
document.all.getElementById("scr").innerHTML=j;
k=document.all.f1.sum.value;
document.all.getElementById("scr").innerHTML=k;
l=document.all.f1.sub;
document.all.getElementById("scr").innerHTML=l;
m=document.all.f1.mul;
document.all.getElementById("scr").innerHTML=m;
n=document.all.f1.div;
document.all.getElementById("scr").innerHTML=n;
o=document.all.f1.clear;
document.all.getElementById("scr").innerHTML=o;
}
</script>
</head>
<body>
<form name="f1" id="f1" method="post">
<div>
<input type="text" id="scr" name="scr" />
</div>
<br/>
<div>
<input type="button" value="7" onclick="display7()" id="seven"name="seven"/>
<input type="button" value="8" onclick="display8()" id="eight"name="eight"/>
<input type="button" value="9" onclick="display9()" id="nine"name="nine"/>
<input type="button" value="/" onclick="display/()" id="div"name="div"/>
<br/>
<input type="button" value="4" onclick="display4()" id="four"name="four"/>
<input type="button" value="5" onclick="display5()" id="five"name="five"/>
<input type="button" value="6" onclick="display6()" id="six"name="six"/>
<input type="button" value="X" onclick="displayX()" id="mul"name="mul"/>
<br/>
<input type="button" value="1" onclick="display()" id="one" name="one" />
<input type="button" value="2" onclick="display2()" id="two"name="two"/>
<input type="button" value="3" onclick="display3()" id="three"name="three"/>
<input type="button" value="-" onclick="display-()" id="sub"name="sub"/>
<br/>
<input type="button" value="CLR" onclick="displayC()"/>
<input type="button" value="0" onclick="display0()" id="zero"name="zero"/>
<input type="button" value="=" onclick="display=()" id="eq"name="eq"/>
<input type="button" value="+" onclick="display+()" id="sum"name="sum"/>
</div>
<br/>
</form>
</body>
</html>
你可以试试这个
HTML
<button id="plus">Plus</button>
<input type="text" id="ih"></input>
Javascript
document.getElementById("ih").value=document.getElementById("plus").innerHTML;
试试这个:
var showValue = function(val){
document.getElementById('pressed').value = parseInt(val);
}
<p><input type="text" id="pressed" value="" /></p>
<p>
<button onclick="showValue(1)">1</button>
<button onclick="showValue(2)">2</button>
<button onclick="showValue(3)">3</button>
</p>
<input type="button" value="test" onClick="document.getElementById('textfield').innerHTML=this.value">
<div id="textfield"></div>