HTML:
<div class="radio" id="uniform-L1"><span><input type="radio" name="length" class="radio" value="L1" id="L1"></span></div>JS:
<script>
if(document.getElementById('L1').checked) {
console.log("chwer");
}
</script>我试着找出我的JS出了什么问题,但我找不到。我没有控制台错误。
发布于 2021-08-13 16:04:57
如果要在每次选中单选按钮时记录change,请侦听"chwer"事件:
document.getElementById('L1').addEventListener('change', function() {
if (this.checked) {
console.log("chwer");
}
})<div class="radio" id="uniform-L1"><span><input type="radio" name="length" class="radio" value="L1" id="L1"></span></div>
发布于 2021-08-13 16:07:52
使用querySelector选择节点
function checkSelection() {
const input = document.querySelector('input[name="length"]');
if (input.checked) {
console.log(input.value);
}
}<div class="radio" id="uniform-L1">
<input type="radio" name="length" class="radio" value="L1" id="L1"> Radio
</div>
<button onclick="checkSelection()">Check</button>
发布于 2021-08-13 16:10:56
我认为问题在于,当您运行JS时,它只会检查单选按钮是否在加载时被选中(不会被选中)。如果希望JS在单选按钮更改时激活,则必须激活一个onclick来检查按钮何时更改,然后运行脚本。
https://stackoverflow.com/questions/68775316
复制相似问题