Javascript 使用 jQuery 更新 HTML H4 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13770562/
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
Update HTML H4 tag using jQuery
提问by three3
I am having trouble changing the "text" between an HTML tag using jQuery. When someone clicks on a "radio button", the text should be updated in a certain HTML element. Here is my HTML:
我在使用 jQuery 更改 HTML 标记之间的“文本”时遇到问题。当有人点击“单选按钮”时,文本应该在某个 HTML 元素中更新。这是我的 HTML:
<div class="radio-line" id="radio-manager">
<input type="radio" id="rad-400" name="radio-manager" value="No" />
</div>
HTML to be updated on radio check:
将在无线电检查中更新的 HTML:
<h4 class="manager">Manager</h4>
When someone clicks on the radio above, the "Manager" text should become "Employees". I tried some jQuery code but cannot quite figure it out.
当有人点击上面的收音机时,“经理”文本应该变成“员工”。我尝试了一些 jQuery 代码,但无法弄清楚。
回答by Dennis Martinez
Check out the following fiddle.
看看下面的小提琴。
<div class="radio-line" id="radio-manager">
<input type="radio" id="rad-400" name="radio-manager" value="No" data-text="Employees" />
</div>
<h4 class="manager">Manager</h4>
$('.radio-line').on('click', 'input[type="radio"]', changeText);
function changeText(e) {
$('.manager').text($(e.currentTarget).data('text'));
}?
回答by Jay Blanchard
All you need to do is this -
你需要做的就是这个——
$('input[name="radio-manager"]').change(function() {
if($('#rad-400').is(':checked')){
$('h4.manager').text('Employees');
} else {
$('h4.manager').text('Manager');
}
});
回答by Jason Towne
From my comment above:
从我上面的评论:
$("h4.manager").text("Employees");
should work.
应该管用。
回答by Rakesh Menon
Does this work?
这行得通吗?
$("#rad-400").on("click", function () {
$(".manager").text("Employees");
});
回答by smykes
This should work for you:
这应该适合你:
$("#rad-r00").click(function() {
if ($('input:radio[name=radio-manager]:checked').val() == "Yes") {
$(".manager").html('Manager')
}
else {
$(".manager").html('Not - Manager');
}
});

