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.