jquery Gets the selected check box value separately

Source: Internet
Author: User

function Jqchk () {//jquery Gets the check box value
var s= ';
$ (' input[name= ' Aihao "]:checked '). each (function () {
s+=$ (This). Val () + ', ';
});

Click "Submit", you can get the correct selection of values, but one more, (comma), this can be detected and then removed with substring, or get to the check box selection value is generally transferred to the group before use, so you can also be transferred to the group after the last array element.
if (s.length > 0) {
Gets the selected CheckBox value sequence
s = s.substring (0,s.length-1);
}
Alert (s== '? ') You have not selected any content yet! ': s);
}
</script>




directly on the code,The main way to get a checkbox value is to put it in an array and concatenate it into a string 


[b]
var chenked=$ ("Input[type= ' checkbox ']:checked"). Val ([]);
var names = "";
for (Var i=0;i<chenked.length;i++) {
Names + = Chenked[i].value + ",";
}

Can be more elegant:

var arr_v = new Array ();

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

Arr_v.push (This.val ());

});

Arr_v.join (', ');

That'll do it.

This is the focus
, the sentence 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. ');
      } 
    } 
    ); [/b]

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;
}
}
 








jquery changes when you select the check box for a background query

var struids= ' ${useridstr} ';//Background Data acquisition
alert (struids);
if (struids!= ")

var str=struids.split (",");
For (var j=0;j<str.length;j++)

$ (": Checkbox[value= '" "+str[j]+" '] "). attr (" Checked ", true);



//check box


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

var s = $ ("#parentId"). Find ("option:selected"). Val ();

jquery Gets the selected check box value separately

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.