使用 JavaScript“this”更改文本框中的文本?

发布于 2024-11-17 00:36:02 字数 1062 浏览 4 评论 0原文

当不同的单选按钮聚焦时,我想更改文本框 id="1" 中的文本。我认为它可以与“this.value”一起使用,但不知何故它不起作用。有人知道如何解决这个问题吗?

问候!

   <head>
     <title>Test</title>
     <script type="text/javascript">
        var a = function(){

            if(this.value == "p"){

                document.getElementById("1").value = "Question product";

            }
            else if(this.value== "v"){

                document.getElementById("1").value = "Question contract";

            }

            else if(this.value== "t"){

                document.getElementById("1").value = "Question technology";

            }

        }
     </script>

   </head>

   <body>
     <input type="radio" name="typ" value="p" onfocus="a()"/> product
     <input type="radio" name="typ" value="v" onfocus="a()"/> contract 
     <input type="radio" name="typ" value="t" onfocus="a()"/> technology 
     <br />
     <input type="text" value="Question" size="46" id="1"/>
   </body>

</html>

I would like to change the text in the textbox id="1" when different radio buttons are focused. I thought it would work with "this.value" but somehow it dosn't. Has someone any idea how to solve this?

Regards!

   <head>
     <title>Test</title>
     <script type="text/javascript">
        var a = function(){

            if(this.value == "p"){

                document.getElementById("1").value = "Question product";

            }
            else if(this.value== "v"){

                document.getElementById("1").value = "Question contract";

            }

            else if(this.value== "t"){

                document.getElementById("1").value = "Question technology";

            }

        }
     </script>

   </head>

   <body>
     <input type="radio" name="typ" value="p" onfocus="a()"/> product
     <input type="radio" name="typ" value="v" onfocus="a()"/> contract 
     <input type="radio" name="typ" value="t" onfocus="a()"/> technology 
     <br />
     <input type="text" value="Question" size="46" id="1"/>
   </body>

</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左秋 2024-11-24 00:36:02

不要使用焦点,而是使用 onclick,请使用数组并为字段提供以字母或下划线开头的 ID

<head>
     <title>Test</title>
     <script type="text/javascript">
      var questionDescription={
        p:"Question product",
        v:"Question contract",
        t:"Question technology"
      }
      window.onload=function(){
        var typ=document.getElementsByName("typ");
        for (var i=0,n=typ.length;i<n;i++) {
          typ[i].onclick=function() {
            document.getElementById("q1").value=questionDescription[this.value]
          }
        }
     }
     </script>

   </head>

   <body>
     <input type="radio" name="typ" value="p" /> product
     <input type="radio" name="typ" value="v" /> contract 
     <input type="radio" name="typ" value="t" /> technology 
     <br />
     <input type="text" value="Question" size="46" id="q1"/>
   </body>

</html>

Do not use focus but onclick and please use an array and give the field an ID beginning with a letter or underscore

<head>
     <title>Test</title>
     <script type="text/javascript">
      var questionDescription={
        p:"Question product",
        v:"Question contract",
        t:"Question technology"
      }
      window.onload=function(){
        var typ=document.getElementsByName("typ");
        for (var i=0,n=typ.length;i<n;i++) {
          typ[i].onclick=function() {
            document.getElementById("q1").value=questionDescription[this.value]
          }
        }
     }
     </script>

   </head>

   <body>
     <input type="radio" name="typ" value="p" /> product
     <input type="radio" name="typ" value="v" /> contract 
     <input type="radio" name="typ" value="t" /> technology 
     <br />
     <input type="text" value="Question" size="46" id="q1"/>
   </body>

</html>
情痴 2024-11-24 00:36:02
input type="radio" name="typ" value="p" onfocus="a(this)"/> product

您需要先将其作为参数传递,然后

var a = function(obj){

        if(obj.value == "p"){

            document.getElementById("1").value = "Question product";

        }
        else if(obj.value== "v"){

            document.getElementById("1").value = "Question contract";

        }

        else if(obj.value== "t"){

            document.getElementById("1").value = "Question technology";

        }

再执行

input type="radio" name="typ" value="p" onfocus="a(this)"/> product

You need to pass this in as an argument first, then do

var a = function(obj){

        if(obj.value == "p"){

            document.getElementById("1").value = "Question product";

        }
        else if(obj.value== "v"){

            document.getElementById("1").value = "Question contract";

        }

        else if(obj.value== "t"){

            document.getElementById("1").value = "Question technology";

        }

instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文