如何在变量中存储文本框值?

时间:2015-07-15 05:36:57

标签: javascript

这是我的剧本:

<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;
        });
</script>

现在请在显示文本框中按钮点击输入的值后告诉我,如何将这些值存储在变量中?

4 个答案:

答案 0 :(得分:1)

<script>
    window.onload = function () {
    var value1= "", value2=""; 
        document.getElementById('btnone').addEventListener('click', function () {
            document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnone').value;
     value1 = document.getElementById('textBox').value;
        });
        document.getElementById('btntwo').addEventListener('click', function () {
            document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btntwo').value;
     value2 = document.getElementById('textBox').value;
        });
</script>

答案 1 :(得分:0)

你可以这样做

     <script>
    var val1,val2; // this is global scope you can use in onload, addEventListener, or in any places of JavaScript of this frame. 
        window.onload = function () {
//if you define two varaibles `var val1,val2;` here they are local to onload and also available for  addEventListener function only.
            var tb = document.getElementById('textBox');
            document.getElementById('btnone').addEventListener('click', function () {
                tb.value = val1 = tb.value + this.value;
            });
            document.getElementById('btntwo').addEventListener('click', function () {
                tb.value = val2 = tb.value + this.value;
            });
    </script>

答案 2 :(得分:0)

<script>
var onestore,twostore;
    window.onload = function () {

        document.getElementById('btnone').addEventListener('click', function () {
            document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btnone').value;
onestore=document.getElementById('textBox').value;
        });
        document.getElementById('btntwo').addEventListener('click', function () {
            document.getElementById('textBox').value = document.getElementById('textBox').value + document.getElementById('btntwo').value;
twostore=document.getElementById('textBox').value;
        });
</script>

答案 3 :(得分:0)

只是声明变量,你可以使用:(变量不在全局范围内,不应该存在)

<script>
    window.onload = function () {
        document.getElementById('btnone').addEventListener('click', function () {
            var btnOneWithTextBox = document.getElementById('textBox').value + document.getElementById('btnone').value
            document.getElementById('textBox').value = btnOneWithTextBox;
        });
        document.getElementById('btntwo').addEventListener('click', function () {
            var btnTwoWithTextBox = document.getElementById('textBox').value + document.getElementById('btntwo').value;
            document.getElementById('textBox').value = btnTwoWithTextBox;
        });
    }
</script>

如果你想在事件之外访问它,那么在这个函数之外创建一个变量,它全部取决于你想要这个变量的范围?

<script>
    window.onload = function () {
        var btnsText;
        document.getElementById('btnone').addEventListener('click', function () {
            btnsText.btnOneWithTextBox = document.getElementById('textBox').value + document.getElementById('btnone').value
            document.getElementById('textBox').value = btnsText.btnOneWithTextBox;
        });
        document.getElementById('btntwo').addEventListener('click', function () {
            btnsText.btnTwoWithTextBox = document.getElementById('textBox').value + document.getElementById('btntwo').value;
            document.getElementById('textBox').value = btnsText.btnTwoWithTextBox;
        });

        //here i can create other events that will use btnsText
    }
</script>