Home >Web Front-end >JS Tutorial >Example of jQuery getting selected checkbox values ​​separately_jquery

Example of jQuery getting selected checkbox values ​​separately_jquery

WBOY
WBOYOriginal
2016-05-16 16:44:091200browse
Copy code The code is as follows:

function jqchk(){ //jquery gets the checkbox 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 after it, (English comma). This can be detected and then removed with substring, or obtained After selecting the value in the check box, it is generally converted into an array before use, so you can also remove the last array element after converting into an array.
Copy code The code is as follows:

if (s.length > 0) {
//Get the selected checkbox value sequence
s = s.substring(0,s.length - 1);
}
alert(s==''?'You haven't selected anything yet! ':s);
}


Go directly to the code, mainly to get the checkbox value: put it into an array and then concatenate it into a string
Copy code The code is as follows:

var chenked=$("input[type='checkbox ']:checked").val([]);
var names = "";
for(var i=0;inames = chenked[i] .value ",";
}

can be more elegant:
Copy code The code is as follows:

var arr_v = new Array();

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

arr_v.push(this.val());

});

arr_v.join(',');

i.e. Okay
Copy code The code is as follows:

//This is the key point, this sentence is the same as the following The first sentence has the same effect
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
Copy code The code is as follows:

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 background query checkbox when modifying jquery
Copy the code The code is as follows:

var struids='${useridstr}'; //Get data in the background
alert(struids);
if(struids!='')
{
var str=struids.split( ",");
for(var j=0;j{
$(":checkbox[value='" str[j] "']"). attr("checked",true);
}
}


Drop-down box
Copy code The code is as follows:

var module='${module}'
$("#module option[value='" module "']").attr ("selected","selected");

var s = $("#parentId").find("option:selected").val();
Statement:
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