Select Gets Drop-down box value dropdown box defaults to select

Source: Internet
Author: User

This article mainly introduces the relative methods of select Drop-down box.

1. Get the value and text values of the dropdown box by ID

For example: <select class= "Form-control" id= "Numbers" >
<option value= "1" > Digital 1</option>
<option value= "2" selected> digital 2</option>
</select>

$ ("#numbers option:selected"). Val (); Gets the Optionde value value to which the Drop-down box is selected: 2;

$ ("#numbers option:selected"). Text (); Gets the Optionde text content that is selected to the dropdown box: number 2;

2. Select a select value by default: Increase the selected property

Enter a number in the input box, call the Selectnumber () method when you lose focus, and select the same number as the input box

function Selectnumber () {
var num = $ ("#num"). Val (); Get the number entered in input
var numbers = $ ("#numbers"). Find ("option"); Get all values for the Select Drop-down box
for (var j = 1; j < Numbers.length; J + +) {
if ($ (numbers[j]). val () = num) {
$ (Numbers[j]). attr ("Selected", "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.