jquery Action Select selects the first element by default

Source: Internet
Author: User

Issue: Click the button to set the default option for the SELECT element.

First, there is an environment and the introduction of jquery.

<!DOCTYPE HTML><HTML>    <Head>        <MetaCharSet= ' Utf-8 '>        <Scriptsrc= ' Jquery-1.11.3.js '></Script>    </Head>    <Body>        <SelectID= ' s1 'class= ' selector '>            <option>1</option>            <option>2</option>            <option>3</option>            <option>4</option>        </Select>                <SelectID= ' s2 'class= ' selector '>            <option>1</option>            <option>2</option>            <option>3</option>            <option>4</option>        </Select>                <Buttononclick= ' resetvalue () '>Click me to reset Select value</Button>        <Script>            functionResetvalue () {}</Script>    </Body></HTML>

The next step is to write a method, in fact, the idea is very understood, using jquery is also very easy to achieve:

1, first find the Select element, here we give him a class convenient to find.

var selectarr = $ ('. selector ');

2. After finding this select array, iterate through the each () method of jquery. The first parameter is the subscript position, and the second parameter is the array element.

Selectarr.each (function  (i, j) {})

3. For each select element, what we need to do is to set the first option tag under it to select

var options = $ (j). Find ("option"); Options.first (). attr (true);

4, open the page, Dot, gee, how a bit of a problem? Ha, set the previously selected item to false before setting the selection. Here I am all false for convenience.

false);

It's done!

The final method content:

function Resetvalue () {    $ ('. Selector '). each (function  (i, j) {        var Options = $ (j). Find ("option");        Options.attr (false);        Options.first (). attr (true);    })}

Again to simplify (set the previous check to false):

function Resetvalue () {    $ ('. Selector '). each (function  (i, j) {        $ (j). Find (false );        $ (j). Find (true);    })}

jquery Action Select selects the first element by default

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.