使用 JavaScript“this”更改文本框中的文本?
当不同的单选按钮聚焦时,我想更改文本框 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用焦点,而是使用 onclick,请使用数组并为字段提供以字母或下划线开头的 ID
Do not use focus but onclick and please use an array and give the field an ID beginning with a letter or underscore
您需要先将其作为参数传递,然后
再执行
You need to pass this in as an argument first, then do
instead