admin管理员组文章数量:1406060
I am currently working with check boxes and displaying values based on the values chosen. Almost everything is working in exception that i have to check boxes for method of shipping
. Right now the user can select two ways and this is an issue. Is there a way to only allow one check box to be checked for 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">
I am currently working with check boxes and displaying values based on the values chosen. Almost everything is working in exception that i have to check boxes for method of shipping
. Right now the user can select two ways and this is an issue. Is there a way to only allow one check box to be checked for 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">
Share
Improve this question
asked Sep 8, 2012 at 18:46
user1434156user1434156
3 Answers
Reset to default 5You should use radio buttons instead:
<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>
The key is the name
attribute, the value should be same for all the radio buttons that belong to one group.
How about radio buttons?
<input type="radio" name="shipping" onclick="check_value(this, 1, '4', 'shipping');">
$(function() {
$('input[type="checkbox"]').bind('click',function() {
$('input[type="checkbox"]').not(this).prop("checked", false);
});
});
本文标签: javascriptCheck boxes Allow for only one check box to be click at a timeStack Overflow
版权声明:本文标题:javascript - Check boxes: Allow for only one check box to be click at a time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744958611a2634511.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论