Jquery operations on forms 2

Source: Internet
Author: User

Cascade Effect of checkbox
Copy codeThe Code is as follows:
<Form>
What kind of sports do you like? <Br/>
<Input type = "checkbox" id = "CheckedAll"/> select all or not <br/>
<Input type = "checkbox" name = "items" value = ""/> football
<Input type = "checkbox" name = "items" value = "basketball"/> basketball
<Input type = "checkbox" name = "items" value = "badminton"/> Badminton
<Input type = "checkbox" name = "items" value = ""/> Ballon
<Input type = "button" id = "send" value = "Submit"/>
$ ('# Checkedall'). click (function (){
$ ('[Name = items]: checkbox'). attr ("checked", this. checked );
})

When you click the check box whose id is "CheckedAll", the check box group is selected. When you deselect an option in the check box group, the check box with id "CheckedAll" is not deselected.
To solve this problem:
Copy codeThe Code is as follows:
$ ('[Name = items]: checkbox'). click (
Function (){
Var flag = true;
$ ('[Name = items]: checkbox'). each (function (){
If (! This. checked)
Flag = false;
});
$ ('# Checkedall'). attr ('checked', flag );
})
// Or the total number of check boxes that can be used is equal to the number of check boxes selected
$ ('[Name = items]: checkbox'). click (
Function (){
Var $ tmp = $ ('[name = items]: checkbox ');
// Use the filter method to filter out the check box and assign a value to CheckedAll.
$ ('# Checkedall'). attr ('checked', $ tmp. length = $ tmp. filter (': checked'). length );
})

Applications in the drop-down list
Copy codeThe Code is as follows:
<Div class = "content">
<Select multiple id = "select1" style = "width: 100px; height: 100px;">
<Option value = "1"> option 1 </option>
<Option value = "2"> option 2 </option>
<Option value = "3"> option 3 </option>
<Option value = "4"> option 4 </option>
<Option value = "5"> option 5 </option>
<Option value = "6"> option 6 </option>
<Option value = "7"> option 7 </option>
</Select>
<Div>
<Span id = "add"> add to right </span>
<Span id = "add_all"> Add all to the right </span>
</Div>
</Div>
<Div class = "content">
<Select multiple id = "select2" style = "width: 100px; height: 100px;">
</Select>
<Div>
<Span id = "add"> add to left </span>
<Span id = "add_all"> Add all to the left </span>
</Div>
<Div>
// Add the selected option to the peer
$ ('# Add'). click (function (){
Var $ options = $ ('# select1 option: selected'); // obtain the selected option
$ Options. appendTo ('# select2'); // append it to the other party
});
// Add all options to the other party
$ ('# Add'). click (function (){
Var $ options = $ ('# select1 option'); // obtain the selected option
$ Options. appendTo ('# select2'); // append it to the other party
});
// Double-click an option to add it to the peer.
$ ('# Select1'). dblclick (function (){
Var $ options = $ ('option: selected', this); // obtain the selected option
$ Options. appendTo ('# select2'); // append it to the other party
})

PS: $ ('option: selected', this), $ () has two parameters: a selector and a scope. Equivalent to $ ('# select1 option: selected ')

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.