如果选中 radio ,我想替换 p 标签值。
我写了一些 JS 来做到这一点,但没有任何改变。 这是我的代码(我使用jquery)
<script>
$(function(){
if ($('#A:checked')) {
$("#change_me").html('<input type="radio" value="1" name="fruit">')
}
if ($('#B:checked')) {
$("#change_me").html('<input type="radio" value="2" name="fruit">')
}
}
</script>
<input type="radio" name="e" id="A" checked="checked">
<input type="radio" name="e" id="B">
<p id="change_me">
<input type="radio" value="1" name="fruit">
</p>
谢谢。
最佳答案
:checked
选择器匹配当前选中的元素。
写入 $('#B:checked')
返回一个包含零个或一个元素的 jQuery 对象。它不能直接用作条件。
相反,您可以检查 if ($('#B:checked').length)
来查看 jQuery 对象中是否包含任何内容。
关于javascript - 如果检查了 radio ,如何替换 p 标签值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5501238/