How to select elements by attribute's value wh

2019-04-20 13:38发布

问题:

I have the following HTML element:

<input type="checkbox" name="ctl00$ContentPH$ucFuncionEdit1$ckEsMenu" />

How can I select all elements with the same name using jQuery, the following fails:

jQuery('[name=ctl00$ContentPH$ucFuncionEdit1$ckEsMenu]');

The previous line of code raise the following error:

Error: Syntax error, unrecognized expression: [name=ctl00$$ContentPH$$ucFuncionEdit1$$ckEsMenu]

回答1:

You quote the value:

jQuery ('[name="ctl00$ContentPH$ucFuncionEdit1$ckEsMenu"]');

When dealing with attribute selectors, it's best to always quote the value (although if the value is a single word containing only the letters A-Z [case insensitive] and the digits 0-9 [but not starting with a digit], you can get away without).



回答2:

You can escape the $ sign with with two backslashes (\\):

jQuery('[name=ctl00\\$ContentPH\\$ucFuncionEdit1\\$ckEsMenu]');



回答3:

you are talking about clientid so you can do it in 2 ways: 1. when your js code in the same page where your html:

jQuery('#<%=ckEsMenu.ClientID%>').click();

2. when your js code is in separeted file:

jQuery("input[id$=ckEsMenu]").click();