有没有人知道为什么我的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";
}
}
答案 0 :(得分:2)
&#34;否则&#34;条件不接受一个语句,它将是IF / ELSE IF接收语句。请参阅更新的代码段!
class Foo
{
public:
Foo();
void bar(int val);
void bar(double val);
// etc.
~Foo();
private:
struct Impl;
Impl* ptr;
};
&#13;
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();
&#13;
答案 1 :(得分:0)
错误来自两个来源。
<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>
setPrice()
。如果没有else (document.getElementById('24').checked)