JQuery gets an example of a checked box value respectively

  • 2020-03-30 03:22:43
  • OfStack

 
function jqchk(){ //Jquery gets the check box value
var s=''; 
$('input[name="aihao"]:checked').each(function(){ 
s+=$(this).val()+','; 
}); 

After clicking "submit", you can get the correct selection value, but there is one more,(English comma), which can be checked and then substring removed, or the checkbox selection value is usually converted to an array for reuse, so you can also remove the last array element after converting to an array.
 
if (s.length > 0) { 
//Gets the selected sequence of checkbox values
s = s.substring(0,s.length - 1); 
} 
alert(s==''?' You haven't chosen anything yet! ':s); 
} 
</script> 

The main way to get the checkbox value is to put it in an array and concatenate it into a string
 
var chenked=$("input[type='checkbox']:checked").val([]); 
var names = ""; 
for(var i=0;i<chenked.length;i++){ 
names += chenked[i].value +","; 
} 

It could be more elegant:
 
var arr_v = new Array(); 

=$("input[type='checkbox']:checked").each(function(){ 

arr_v.push(this.val()); 

}); 

arr_v.join(','); 

Which can be the
 
//It is the same as the first sentence below
var selectedItems = new Array(); 
$("input[@name='itemSelect[]']:checked").each(function() {selectedItems.push($(this).val());}); 

if (selectedItems .length == 0) 
alert("Please select item(s) to delete."); 
else 
$.ajax({ 
type: "POST", 
url: "/ajax_do_something.php", 
data: "items=" + selectedItems.join('|'), 
dataType: "text", 
success: function (request) { 
document.location.reload(); 
}, 
error: function(request,error){ 
alert('Error deleting item(s), try again later.'); 
} 
} 
); 

Java split
 
String names = null; 
String name1 = null; 
String name2 = null; 
names = request.getParameter("names"); 
String[] name = names.split(","); 
for(String x : name){ 
if("zhangsan".equals(x)){ 
name1 = x; 
} 
if("lisi".equals(x)){ 
name2 = x; 
} 
} 

Select the check box for background queries when jquery changes
 
var struids='${useridstr}'; //Background fetch data
alert(struids); 
if(struids!='') 
{ 
var str=struids.split(","); 
for(var j=0;j<str.length;j++) 
{ 
$(":checkbox[value='"+str[j]+"']").attr("checked",true); 
} 
} 


A drop-down box
 
var module='${module}' 
$("#module option[value='" + module + "']").attr("selected","selected"); 

var s = $("#parentId").find("option:selected").val(); 

Related articles: