jquery 2_jquery on Form operations

Source: Internet
Author: User
cascading Effects of a checkbox
Copy Code code as follows:

<form>
The sport you love? <br/>
<input type= "checkbox" id= "Checkedall"/> Select all/Do not select <br/>
<input type= "checkbox" name= "Items" value= "Football"/> Football
<input type= "checkbox" name= "items" value= "basketball"/> Basketball
<input type= "checkbox" name= "Items" value= "Badminton"/> Badminton
<input type= "checkbox" name= "Items" value= "Table tennis"/> Pong ball
<input type= "button" id= "send" value= "Submit"/>
$ (' #CheckedAll '). Click (function () {
$ (' [Name=items]:checkbox '). attr ("Checked", this.checked);
})

When you click the check box with id "Checkedall", the checkbox group is selected, and the check box with the id "Checkedall" is not unchecked when an option is checked in the checkbox group.
To address this problem:
Copy Code code 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 you can use the total number of check boxes equal to the number of selected check boxes
$ (' [Name=items]:checkbox '). Click (
function () {
var $tmp = $ (' [Name=items]:checkbox ');
Filter out check boxes using the Filter method and assign values directly to Checkedall
$ (' #CheckedAll '). attr (' checked ', $tmp. length = = $tmp. Filter (': Checked '). length);
})

The application of the dropdown box
Copy Code code 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" > select Add to Right </span>
<span id= "Add_all" > All add to Right </span>
</div>
</div>
<div class= "Content" >
<select multiple id= "Select2" style= "width:100px;height:100px"; >
</select>
<div>
<span id= "Add" > select Add to Left </span>
<span id= "Add_all" > All add to left </span>
</div>
<div>
Add selected options to each other
$ (' #add '). Click (function () {
var $options = $ (' #select1 option:selected ');//Get selected option
$options. Appendto (' #select2 ');//append to each other
});
Add all the options to each other
$ (' #add '). Click (function () {
var $options = $ (' #select1 option ');//Get selected option
$options. Appendto (' #select2 ');//append to each other
});
Double-click an option to add to each other
$ (' #select1 '). DblClick (function () {
var $options = $ (' option:selected ', this);//Get selected option
$options. Appendto (' #select2 ');//append to each other
})

PS: $ (' option:selected ', this), $ () has 2 parameters, one is selector, one is 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.