开发者

Change a text in textbox with JavaScript "this"?

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>


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜