谁能帮我写一个简单的 jQuery 代码,当点击不同的单选按钮时显示不同的内容。
http://jsfiddle.net/AXsVY/
HTML
<label class="radio inline">
<input id="up_radio" type="radio" name="optionsRadios" value="update" checked>
Update
</label>
<label class="radio inline">
<input id="ov_radio" type="radio" name="optionsRadios" value="overwritten">
Overwritten
</label>
<p id="cont">
sth. here
</p>
JavaScript
$("#up_radio").click(function(){
if ($("#up_radio").is(":checked")) {
//change to "show update"
$("#cont").value = "show update";
} else if ($("#ov_radio").is(":checked")) {
// change to "show overwritten"
}
});
最佳答案
在更改时使用,而不是在单击时使用。单击触发器会发生变化,但用户也可以切换并按下箭头键。它总是改变
。
$('input[name="optionsRadios"]').on('change', function(){
if ($(this).val()=='update') {
//change to "show update"
$("#cont").text("show update");
} else {
$("#cont").text("show Overwritten");
}
});
此外,设置值的正确语法是 $("#something").val("the value");
但在这种情况下 #cont 是一个 div,因此您需要使用 .text()
或 .html()
。
关于javascript - 选择单选按钮时 jQuery 更改内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15262025/