Example of jQuery getting the selected check box values

Source: Internet
Author: User

Copy codeThe Code is as follows:
Function jqchk () {// jquery get the check box Value
Var s = '';
$ ('Input [name = "aihao"]: checked'). each (function (){
S + = $ (this). val () + ',';
});

After you click "Submit", you can get the correct selection value, but there will be one more (comma) next to it. This can be checked and removed with substring, or after obtaining the selection value of the check box, it is usually converted to an array before use. Therefore, you can also convert it to an array to remove the last array element.
Copy codeThe Code is as follows:
If (s. length> 0 ){
// Obtain the sequence of the selected checkbox values
S = s. substring (0, s. length-1 );
}
Alert (s = ''? 'You have not selected any content! ': S );
}
</Script>

Directly put the code, mainly to get the checkbox value: Put it in the array, and then connect it to a string
Copy codeThe Code is as follows:
Var chenked = $ ("input [type = 'checkbox']: checked"). val ([]);
Var names = "";
For (var I = 0; I <chenked. length; I ++ ){
Names + = chenked [I]. value + ",";
}

It can be more elegant:
Copy codeThe Code is as follows:
Var arr_v = new Array ();

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

Arr_v.push (this. val ());

});

Arr_v.join (',');

That's it.
Copy codeThe Code is as follows:
// This is the focus. The effect of this sentence is the same as that of 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 splitting
Copy codeThe 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;
}
}

Check box for background Query when jquery is modified
Copy codeThe Code is as follows:
Var struids = '$ {useridstr}'; // obtain data in the background
Alert (struids );
If (struids! = '')
{
Var str = struids. split (",");
For (var j = 0; j <str. length; j ++)
{
$ (": Checkbox [value = '" + str [j] + "']"). attr ("checked", true );
}
}


Drop-down list
Copy codeThe Code is as follows:
Var module = '$ {module }'
$ ("# Module option [value = '" + module + "']"). attr ("selected", "selected ");

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

Related Article

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.