标签云

微信群

扫码加入我们

WeChat QR Code

Have a table column I'm trying to expand and hide: jQuery seems to hide the td elements when I select it by class but not by element's name.For example, why does: $(".bold").hide(); // selecting by class works$("tcol1").hide(); // select by element name does not workNote the HTML below, the second column has the same name for all rows.How could I create this collection using the name attribute?<tr><td>data1</td><td name="tcol1" class="bold"> data2</td></tr><tr><td>data1</td><td name="tcol1" class="bold"> data2</td></tr><tr><td>data1</td><td name="tcol1" class="bold"> data2</td></tr>


Question does not match content. ID and name are different attributes and are selected differently

2019年05月24日13分12秒

It's against W3C standards to have elements with the same ID; i.e. duplicate IDs are a no no.

2019年05月23日13分12秒

Just out of curiosity, how would one write this as NOT tcol1, like what if they wanted to hide the columns other than the columns named, tcol1?

2019年05月24日13分12秒

Jon Erickson - what if i dont knowthe element tag(td in this case)as well..

2019年05月23日13分12秒

Varun - you can just omit the td... for example $('[name^=tcol]') will match all elements that have an attribute 'name' with a value that starts with 'tcol'

2019年05月24日13分12秒

Varun - you can see it in action here: jsfiddle.net/zdPME

2019年05月24日13分12秒

HPWD you would use the :not selector, $("td:not([name='tcol1'])") for example. You can see it at this fiddle

2019年05月24日13分12秒

but you should use the above code, so that you donot miss up with the qoutations! Good luck and welcome to Stackoverflow community :)

2019年05月23日13分12秒

This is not working at least for me - But following statement works var value = $("[name=nameofobject]");

2019年05月24日13分12秒

It's surprising this get 17 upvotes when this came 4 years after the original accepted answer

2019年05月23日13分12秒

21 upvotes now...maybe it's because the answer is less confusing than 'td[name=tcol1]', especially since the td is not required and hence leads people like me down the wrong path

2019年05月24日13分12秒

My input names had a [] like this: <input name="itemid[]">. So this answer worked more perfectly than the accepted answer, thanks to the proper usage of quotes.

2019年05月23日13分12秒

+1 The only example given which selects a name field including type and limits the results to within an id section.

2019年05月24日13分12秒

I agree. You can have multiple forms in your page, and restricting to the proper one is a good idea.

2019年05月24日13分12秒

Literally the answer given above: stackoverflow.com/a/18947703/439094

2019年05月24日13分12秒

Welcome to Stack Overflow, sumith deepan, your answer seems alright, but would you mind explaining what your code does and why?

2019年05月24日13分12秒

for example if the field name is 'td[name="nested[fieldName]"]'

2019年05月23日13分12秒

This is very true.Newer versions of jQuery (v. 3.2.1) are much more likely to fail when encountering a attribute-based selector element without proper quoting.

2019年05月24日13分12秒

Somewhat wordy answer, but this is fundamentally the correct overall approach.

2019年05月24日13分12秒

May be a bit wordy, but better to have a bit extra explanation and reasoning behind a suggestion than a single line that explains little! ;) Its useful when someone is new to something and trying to understand why a) it works and b) how it works

2019年05月23日13分12秒

Matches a single element only

2019年05月24日13分12秒

Sorry about that. Just do what Ben S said.

2019年05月24日13分12秒

Yes that is true about the ID. As far as selecting by name, I think there was a post up here referring to Selectors/attributeEquals which was helpful.

2019年05月23日13分12秒

Maybe the question has been edited, but he is asking for name reference now, but yes single elements can use the id selector

2019年05月24日13分12秒

What value does this answer add that other answers have not yet provided?

2019年05月24日13分12秒

this is a lazy dupe of the already accepted answer.

2019年05月24日13分12秒