简体   繁体   English

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

[英]Change label text on radio button selection with jQuery

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? 从“设置默认”设置为“默认”时,我需要更改单选按钮上的标签文本-如何使用jQuery完成? I presume that you would use the class radio-default as the selector but I don't know much about 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> 

Try 尝试

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

Fiddle 小提琴

Use below script 使用以下脚本

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

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

});

You can add an on click hadler on the radio button 您可以在单选按钮上添加一个点击式哈德勒

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

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

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

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");
})

Try this 尝试这个

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

http://jsfiddle.net/ph2gvjcc/ http://jsfiddle.net/ph2gvjcc/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM