Javascript如何改变单选按钮标签文字? [英] Javascript how to change radio button label text?

查看:98
本文介绍了Javascript如何改变单选按钮标签文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改与单选按钮id =male关联的标签的文字。我尝试过不同的方式来做到这一点,但我无法做到这一点。我想在与单选按钮关联的标签中更改文字男。

 < input type =radio name =sexid =malevalue =male> 
< label for =male>男性< / label>
< / input>

< input type =buttonvalue =Submitonclick = test()>

< script>

函数test()
{
var r = document.getElementById(male);
r.nextSibling.data =adaS;
// r.nextSibling.nodeValue =adaS; //已经尝试了所有这些方法
// r.childNodes [0] .value =adaS;
// r.childNodes [0] .innerHTML =adaS;
// r.parentNode.childNodes [1] .innerHTML =adaS;

}

< / script>

请建议一些可行的方法来更改标签中的文字男性。

$您可以使用

  var r = <$ p 
$ div class =h2_lin>解决方案

document.getElementsByTagName(label)

选择页面中的所有标签元素,然后使用

  r [0] .innerHTML =新文本

选择第一个标签并将文本设置为test()函数中的下一个文本。

I want to change the text of the label which is associated with the radiobutton id="male". I have tried various ways to do it, but i can't make it work. I want to change the text "Male" in the label associated to the radio button.

  <input type="radio" name="sex" id="male" value="male">
        <label for="male">Male</label>
  </input>

 <input type="button" value="Submit" onclick = test()>

<script>

function test()
{
   var r = document.getElementById("male");
   r.nextSibling.data = "adaS";
//  r.nextSibling.nodeValue = "adaS";     // have tried all these ways
//  r.childNodes[0].value= "adaS";
//  r.childNodes[0].innerHTML= "adaS";
//   r.parentNode.childNodes[1].innerHTML= "adaS";

}

</script>

please suggest some working way to change the text "Male" in the label.

解决方案

You can use

var r = document.getElementsByTagName("label")   

to select all the label element in your page and then use

r[0].innerHTML ="new text" 

to select first label and set the text to "next text" in your test() function

这篇关于Javascript如何改变单选按钮标签文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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