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
Change label text on radio button selection with jQuery
提问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-default
as 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
回答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
回答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');
});
回答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');
}