如何在变量中存储文本框值? [英] How Can I Store Textbox Value In Variable?
本文介绍了如何在变量中存储文本框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有单个文本框。单击按钮时,按钮值显示在文本框中。我想要的是将文本框的值存储在某个变量中,以便稍后我可以添加这些值。
这是我的代码:
I have single textbox. when buttons are clicked button value are displayed in textbox. What i want is to store value of textbox in some variable so that i can add these values later.
Here is my code:
<html>
<head>
<title>
</title>
<script>
window.onload = function() {
document.getElementById('btnone').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnone').value;
});
document.getElementById('btntwo').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btntwo').value;
});
document.getElementById('btnthree').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnthree').value;
});
document.getElementById('btnfour').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnfour').value;
});
document.getElementById('btnadd').addEventListener('click', function(){
document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnadd').value;
});
}
</script>
</head>
<body>
<input type="button" name="btnone" id="btnone" value="1" onclick="setText1()" />
<input type="button" name="btntwo" id="btntwo" value="2" onclick="setText2()" />
<input type="button" name="btnthree" id="btnthree" value="3" onclick="setText3()" />
<input type="button" name="btnfour" id="btnfour" value="4" onclick="setText4()" />
<br/>
<input type="button" name="btnadd" id="btnadd" value="+" onclick="setText+()" />
<input type="button" name="btneql" id="btneql" value="=" onclick="setText=()" />
<input type="text" name="textBox" id="textBox" value=""/>
</body>
</html>
推荐答案
var txtvalue = document.getElementById('textBox')。value;
var txtvalue=document.getElementById('textBox').value;
如果你正在使用jQuery你也可以使用..
if you're using jQuery you could also use..
(' #textBox')。val();
('#textBox').val();
这篇关于如何在变量中存储文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文