Home > Web Front-end > JS Tutorial > body text

input:checkbox multi-select box achieves the same single selection effect as radio

高洛峰
Release: 2018-05-17 17:04:28
Original
2723 people have browsed it

I recently encountered a small problem, that is, sometimes when we use single-select radio, we will find that after we select one, we can no longer select none, that is, there is no cancel function after selecting. Then I thought of the powerful checkbox, but it has multiple selections. How can I turn it into a single selection? The effect is the same as radio. I wrote a small program myself. The code is very simple, only four lines.
No more nonsense, direct code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script src="js/jquery-1.8.3.min.js" type="text/javascript" language="javascript"></script> 
<title>无标题文档</title> 
<script type="text/javascript"> 
$(function(){ 
$(":checkbox").click(function(){ 
if($(this).attr("checked")!=undefined) 
{ 
$(this).siblings().attr("checked",false); 
$(this).attr("checked",true); 
} 
}); 
}); 
</script> 
<style> 
span,input{float:left;} 
input{ width:14px; height:14px;} 
span{ margin-right:20px;} 
</style> 
</head> 

<body> 
<p> 
<input type="checkbox" /><span>1</span> 
<input type="checkbox" /><span>2</span> 
<input type="checkbox" /><span>3</span> 
<input type="checkbox" /><span>4</span> 
<input type="checkbox" /><span>5</span> 
<input type="checkbox" /><span>6</span> 
<input type="text" /><span>7</span> 
</p> 
</body> 
</html>
Copy after login

More input: checkbox multi-select box to achieve the same single-select effect as radio. For related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template