通过单击按钮在Javascript中填充文本框 [英] Populate text box in Javascript by clicking on button

查看:98
本文介绍了通过单击按钮在Javascript中填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过单击表单按钮来填充表单上的文本框。下面是我目前使用的代码 - 从选择框示例中修改了这段代码 -

I am trying to populate a text box on a form by clicking on form buttons. Below is code I have so far - modified this code from a select box example -

<!DOCTYPE html>
<html>

<head>
    <script>
        function moveNumbers(){
            var no=document.getElementById("no");
            var txt=document.getElementById("result").value;
            txt=txt + option;
            document.getElementById("result").value=txt;
        }
    </script>
</head>

<body>
    <form>
        Select numbers:<br>
        <input type="button" value="1" name="no" onclick="moveNumbers()"> 
        <input type="button" value="2" name="no" onclick="moveNumbers()"> 
        <input type="button" value="3" name="no" onclick="moveNumbers()"> 

        <input type="text" id="result" size="20">
    </form>
</body>

</html>


推荐答案

这里有一些缺陷。它似乎没有定义选项。您无法检索实际点击的按钮。

There are a few flaws here. It doesn't seem like option is defined. And you have no way to retrieve the button that was actually clicked.

您可以做的是传递 this.value 到你的onclick事件处理程序。这会传递您按下的按钮的值,并使用该值添加到您的文本框值。

What you can do is pass this.value to your onclick event handler. This passes the value of the button you push, and use that to append to your textbox value.

<script> function moveNumbers(num) {
    var txt=document.getElementById("result").value;
    txt=txt + num;
    document.getElementById("result").value=txt;
    }
</script>
Select numbers: <br> <input type="button" value="1" name="no" onclick="moveNumbers(this.value)">  
<input type="button" value="2" name="no" onclick="moveNumbers(this.value)">  
<input type="button" value="3" name="no" onclick="moveNumbers(this.value)">  
<input type="text" id="result" size="20">

http:// jsfiddle.net/cMN44/

这篇关于通过单击按钮在Javascript中填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆