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