Method that allows the checkbox to select only one item

  • 2020-03-26 21:22:13
  • OfStack

Js:
 
function chooseOne(chk){ 
//Get the collection object of chekcBox with the same name
var obj = document.getElementsByName("chk"); 
for (i=0; i<obj.length; i++){ 
//Determine whether the I element in the I collection has a cb, and if not, it is not
if (obj[i]!=chk) obj[i].checked = false; 
//To tick at least one tick, remove the else from the top row and use the bottom row
else obj[i].checked = true; 
} 
} 

In the JSP page:
 
<input type="checkbox" id="${userGroup.groupId}" name="chk" onClick="chooseOne(this)"> 

Related articles: