jquery Select dropdown box to find Value_jquery by text

Source: Internet
Author: User

The data that is usually obtained from the database requires the dropdown box text and value to be converted to each other

I've introduced a corresponding article earlier.

jquery Select to find text by value

This time, in turn, the jquery Select dropdown box uses text to find value

Find ways to try, and finally feel the most reliable use of cyclic option

Unless <option value= "0" text= "all" > All </option>

This is written in

$ ("#orderstate option[text= ' All ']"). Val ();

Look at the HTML

            <th> Work order status:</th>
            <td>
                <select id= "orderstate" class= "select" style= "width:108px" >
                    <option value= "-1" > All </option>
                    <option value= "0" > Not audited </option>
                    <option Value= "1" > Unfinished </option>
                    <option value= "2" > has been completed </option>
                </select>
            </td >

So it doesn't make sense to use the above method, you can't find the value

The following loop is used to fix

var ordersate = $ ("#orderstate option[text= '" + state + "']"). Val ();
The jquery Select dropdown box uses text to find the value
function selectvalue (obj, text) {
var val = "";
$ ("#orderstate option"). each (the function () {
if ($ (this). Text () = = text) {
val = $ (This)                . val ();
return false;
}
});
return Val;
}


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.