JQuery contains filter for exact matching

Source: Internet
Author: User

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> </title>
<! -- <Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type = "text/javascript"> </script> -->
<Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Function (){
// Select an option based on the option text in the select statement.
// $ ("# Selectbox option [text = 'second project']");
// $ ("# Selectbox option"). filter ("[text = 'second project']");
// Both of the above statements are incorrect.
// Write the statement correctly
$ ("# Btn4"). click (function (){
Var $ option = $ ("# selectbox option: contains ('second Project')"). map (function (){
If ($ (this). text () = "second "){
Return this;
}
});
Alert ($ option. length> 0? "Object": "No object ");
$ Option. attr ("selected", true );
});
});
</Script>
</Head>
<Body>
<Form id = "form1">
<Div>
<Select id = "selectbox">
<Option value = "1"> Item 1 </option>
<Option value = "2"> option 2 </option>
<Option value = "21"> option 2 </option>
</Select>
<Input type = "button" id = "btn4" value = "contains test"/>
</Div>
</Form>
</Body>
</Html>

$ (". Selector: contains ('xx ')")
Contains () is used only for matching searches. It is not accurate enough. The selector that contains xx and the selector that contains xxabc will be checked.

Solution:
? $ (". Selector: contains ('xx') [innerHTML = 'xx']")
In this way, only the selector of xx is found.

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.