gpt4 book ai didi

javascript - 简单的JS函数不起作用

转载 作者:行者123 更新时间:2023-12-01 03:38:21 25 4
gpt4 key购买 nike

有人知道为什么我的 JS 函数不起作用/我的价格 div 根本不显示任何内容吗?

HTML:

<div id="variant">
<label><input type="radio" name="toggle" id="3"><span>A</span></label>
<label><input type="radio" name="toggle" id="12" checked="checked"><span>B</span></label>
<label><input type="radio" name="toggle" id="24"><span>C</span></label>
</div>
<br>
<div id="price"></div>

JS:

function setPrice() {
if (document.getElementById('3').checked) {
document.getElementById('price').innerHTML = "19,99€";
} else if (document.getElementById('12').checked) {
document.getElementById('price').innerHTML = "<<<";
} else (document.getElementById('24').checked) {
document.getElementById('price').innerHTML = "xxx";
}
}

最佳答案

“else”条件不接受语句,而是 IF/ELSE IF 接受语句。请查看更新的代码片段!

function setPrice() {
if (document.getElementById('3').checked) {
document.getElementById('price').innerHTML = "19,99€";
} else if (document.getElementById('12').checked) {
document.getElementById('price').innerHTML = "<<<";
} else if (document.getElementById('24').checked) {
document.getElementById('price').innerHTML = "xxx";
}
}

setPrice();
<div id="variant">
<label><input type="radio" name="toggle" id="3" onClick="setPrice();"><span>A</span></label>
<label><input type="radio" name="toggle" id="12" onClick="setPrice();" checked="checked"><span>B</span></label>
<label><input type="radio" name="toggle" id="24" onClick="setPrice();"><span>C</span></label>
</div>
<br>
<div id="price"></div>

关于javascript - 简单的JS函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44078474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com