javascript 使用 jQuery 更改单选按钮选择上的标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30664793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:31:10  来源:igfitidea点击:

Change label text on radio button selection with jQuery

javascriptjqueryhtml

提问by Simon

I need to change the label text on a radio button when it is selected from "Set default" to "default" - How can this be done with jQuery? I presume that you would use the class radio-defaultas the selector but I don't know much about jQuery.

当从“设置默认值”选择为“默认值”时,我需要更改单选按钮上的标签文本 - 如何使用 jQuery 完成此操作?我假设您会使用该类radio-default作为选择器,但我对 jQuery 了解不多。

<div class="radio-default">
  <input type="radio" id="line-radio-1" name="line-radio">
  <label for="line-radio-1">Set default</label>           
</div>

<div class="radio-default">
  <input type="radio" id="line-radio-2" name="line-radio">
  <label for="line-radio-2">Set default</label>           
</div>

<div class="radio-default">
  <input type="radio" id="line-radio-3" name="line-radio">
  <label for="line-radio-3">Set default</label>           
</div>

采纳答案by Sudharsan S

Try

尝试

$("input[name='line-radio']").click(function() {
      $('.radio-default label').text('Set default');
      if(this.checked){
       $(this).next().text('default');
      }
 });

Fiddle

小提琴

回答by Vivek Gupta

Use below script

使用下面的脚本

 $( "input:radio" ).click(function() {
    $(':radio').each(function() {
        $("#"+this.id).next().text( "Set default" );
    });

    $("#"+this.id).next().text( "Default" );

});

回答by Abhishek Jain

Do this:-

做这个:-

 var $radioButtons = $('.radio-default input:radio[name="line-radio"]');
 $radioButtons.change(function(){
    if($(this).val() == 'on'){
       $radioButtons.siblings('label').text('Set default');
       $(this).siblings('label').text('default')
    }
});

Working JsFiddle here

在这里工作 JsFiddle

回答by Monicka

you may want to reset the text on the previoused button clicked, but this can help you:

您可能希望重置单击的上一个按钮上的文本,但这可以帮助您:

$('.radio-default').on('click',function(){
    $(this).find('label').text("set");
})

回答by Newinjava

Try this

试试这个

$('input:radio').click(function() {
   $('label').text('Set default');
   $(this).next('label').html('Default');
  });

http://jsfiddle.net/ph2gvjcc/

http://jsfiddle.net/ph2gvjcc/

回答by Wesley Skeen

You can add an on click hadler on the radio button

您可以在单选按钮上添加一个点击hadler

<input type="radio" id="line-radio-1" name="line-radio" onClick="yourFunc">

yourFunc: function(e){
    $(e).next('label').text('your text');
}