Example of Javascript to get the value of the drop-down list

Source: Internet
Author: User

The core statement for getting the value of the drop-down list using js is: document. getElementById (id). options [Objsel. selectedIndex]. value;, but jquery is simpler.

Js to get the value of the drop-down list

The Code is as follows: Copy code


<Fieldset>
<Label> type </label>
<Select name = "pid" id = 'pid '>
<Option value = "0"> select </option>
<Option value = "1"> PHP Learning Network </option>
<Option value = "2"> PHP Learning Network </option>
</Select>
</Fieldset>
<Fieldset>
<Label> PHP Learning Network </label>
<Select name = "sfk" id = 'sfk'>
<Option value = "0"> select </option>
<Option value = "0.3"> 30% </option>
<Option value = "0.4"> 40% </option>
<Option value = "0.5"> 50% </option>
<Option value = "0.6"> 60% </option>
</Select>
</Fieldset>
<Fieldset>
<Label> PHP Learning Network http://www.bKjia. c0m </label>
<Select name = "yfk" id = 'yfk '>
<Option value = "0"> select </option>
<Option value = "1"> 1 year </option>
<Option value = "2"> 2 years </option>
<Option value = "3"> 3 years </option>
<Option value = "5"> 5 years </option>
</Select>
</Fieldset>
<Fieldset>
<Label> & nbsp; </label>
<Input type = "button" onClick = "sikp ('pid ', 'sfk', 'yfk')"/>
</Fieldset>
</Div>

<Script type = "text/javascript">
Function sikp (p, s, y)
{
Var pid = getselval (p );
Var sfk = getselval (s );
Var yfk = getselval (y );
Window. open ("http://www.hzhuti.com" + pid + sfk + yfk); // link to a different place to add that value
}

// Obtain the value of the drop-down list
Function getselval (id)
{
Var Objsel = document. getElementById (id)
Var selval = Objsel. options [Objsel. selectedIndex]. value;
Return selval;
}
</Script>


Jquery obtains Select:
 
Obtain the selected text in the select statement:

The Code is as follows: Copy code
$ ("# DdlRegType"). find ("option: selected"). text ();

Obtain the value selected by the select statement:

The Code is as follows: Copy code
$ ("# DdlRegType"). val ();

Obtain the index selected by the select statement:

The Code is as follows: Copy code
$ ("# DdlRegType"). get (0). selectedIndex;

Set select:
Set the index selected by select:

The Code is as follows: Copy code
$ ("# DdlRegType"). get (0). selectedIndex = index; // index

Is the index value

Related Article

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.