jquery takes the value of the selected checkbox

Source: Internet
Author: User
Tags api manual

One. in the HTML checkbox, the selected word will have the attribute checked= "checked ".

If a checkbox is selected, alert the CheckBox's property "checked" with the value alert ($ "#xxx". attr ("checked")), which prints "true" instead of " Checked "!

If not selected, the print is "undefined ". That's weird, isn't it? Keep looking down ~

Do not attempt to make such a judgment: if ($ "#xxx". attr ("checked") = = "true ") //This is wrong

because it's wrong to do this, the jquery API manual says that the return value of attr (name) is object. Therefore, it should be if ($ ("#xxx"). attr ("checked") = =true).

Determine the value of $ ("Input[name= ' Weibo_count ')"). attr ("checked"); that's fine.

"http://www.w3.org/1999/xhtml">
"Server">
<script language="JavaScript">
<!--
$("Document"). Ready (function () {

$("#btn1"). Click (function () {

$("[name= ' checkbox ']"). attr ("checked",'true');//Select All

})
$("#btn2"). Click (function () {

$("[name= ' checkbox ']"). Removeattr ("checked");//Cancel Select all

})
$("#btn3"). Click (function () {

$("[name= ' checkbox ']:even"). attr ("checked",'true');//Check all odd

})
$("#btn4"). Click (function () {

$("[name= ' checkbox ']"). each (function () {


if($( This). attr ("checked"))
{
$( This). Removeattr ("checked");

}
Else
{
$( This). attr ("checked",'true');

}

})

})
$("#btn5"). Click (function () {
varChecks ="";
$("input[name= ' checkbox[]"). each (function () {
if($( This). attr ("checked") ==true){
Checks + = $ ( This). Val () +"|";//dynamically spell the value of the selected checkbox with the ' | ' Symbol Separation
}
})
})
// -
</SCRIPT>

</HEAD>
<BODY>
<form name="Form1"Method="Post"action="">
<input type="Button"Id="BTN1"Value="Select All">
<input type="Button"Id="btn2"Value="Cancel Select all">
<input type="Button"Id="Btn3"Value="Check all odd">
<input type="Button"Id="Btn4"Value="Inverse Selection">
<input type="Button"Id="Btn5"Value="get all the values selected">
<br>
<input type="checkbox"Name="checkbox[]"Value="CheckBox1">
CheckBox1
<input type="checkbox"Name="checkbox[]"Value="CheckBox2">
CheckBox2
<input type="checkbox"Name="checkbox[]"Value="Checkbox3">
Checkbox3
<input type="checkbox"Name="checkbox[]"Value="checkbox4">
Checkbox4
</form>
</body>

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.