我目前正在使用复选框,并根据所选值显示值。几乎所有事情都在异常(exception),我必须选中method of shipping
复选框。现在,用户可以选择两种方式,这是一个问题。有没有办法只允许选中一个复选框的method of shipping
? EXAMPLE
JS
<script>
function check_value(currentElement, val, id, type) {
var el = document.getElementById("imgBox" + id);
if (val > 0 && val < 4) { //will trigger when [1,2,3]
if(currentElement.checked)
{
el.src = "images/" + type + val + ".jpg";
el.style.display = "";
}
else
{
el.src = "";
el.style.display = "none";
}
}
}
</script>
HTML
<h2>Choose method of shipping</h2>
<form name="shipping_list">
<input type="checkbox" name="shipping1" onclick='check_value(this, 1, "4", "shipping")'/> USPS Ground<br />
<input type="checkbox" name="shipping2" onclick='check_value(this, 2, "4", "shipping")'/> FedEx
</form>
<img id="imgBox4" src="#" style="display:none">
最佳答案
您应该改用单选按钮:
<form name="shipping_list">
<input type="radio" name="shipping" id="shipping1" onclick='check_value(this, 1, "4", "shipping")'/> USPS Ground<br />
<input type="radio" name="shipping" id="shipping2" onclick='check_value(this, 2, "4", "shipping")'/> FedEx
</form>
关键是
name
属性,该值对于属于一组的所有单选按钮应该相同。
关于javascript - 复选框:一次只允许单击一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12333503/