As of jQuery 1.9 the .selector
property of jQuery objects has been removed. (I'm a little confused as to why, exactly). I actually use it in a few unique scenarios, and I know that I could do other things to prevent this. Just wondering if anyone knows another way of grabbing the selector as of 1.9?
$('#whatever').selector // (would of returned '#whatever')
One example of where I need .selector is when I already have a group of checkboxes by name, and I want to see, within that group, which one is checked:
jsFiddle DEMO
var $test = $('input[name="test"]');
console.log( $test );
console.log( $(':checked', $test).attr('id') ); // returns --undefined--
console.log( 'now with .selector: ');
console.log( $($test.selector + ':checked').attr('id') ); // returns correct
From the docs: .selector property on jQuery objects
The remaining purpose of the deprecated .selector property on a jQuery object is to support the deprecated .live() event. In 1.9, jQuery no longer attempts to maintain this property in chained methods, since the use of chained methods was never supported with .live(). Do not use the .selector property on a jQuery object. The jQuery Migrate plugin does not attempt to maintain this property.
As of jQuery 1.9 the .selector
property of jQuery objects has been removed. (I'm a little confused as to why, exactly). I actually use it in a few unique scenarios, and I know that I could do other things to prevent this. Just wondering if anyone knows another way of grabbing the selector as of 1.9?
$('#whatever').selector // (would of returned '#whatever')
One example of where I need .selector is when I already have a group of checkboxes by name, and I want to see, within that group, which one is checked:
jsFiddle DEMO
var $test = $('input[name="test"]');
console.log( $test );
console.log( $(':checked', $test).attr('id') ); // returns --undefined--
console.log( 'now with .selector: ');
console.log( $($test.selector + ':checked').attr('id') ); // returns correct
Share Improve this question edited Feb 19, 2013 at 18:36 Mark Pieszak - Trilon.io asked Feb 19, 2013 at 18:28 Mark Pieszak - Trilon.ioMark Pieszak - Trilon.io 67.3k15 gold badges83 silver badges96 bronze badges 10From the docs: .selector property on jQuery objects
The remaining purpose of the deprecated .selector property on a jQuery object is to support the deprecated .live() event. In 1.9, jQuery no longer attempts to maintain this property in chained methods, since the use of chained methods was never supported with .live(). Do not use the .selector property on a jQuery object. The jQuery Migrate plugin does not attempt to maintain this property.
- 5 mind providing one of those few unique scenarios? (code, not description, please) – PlantTheIdea Commented Feb 19, 2013 at 18:29
-
2
$('#whatever').selector
still seems to work. The documentation says "In 1.9, jQuery no longer attempts to maintain this property in chained methods [...]". Though api.jquery./selector claims it was removed. I don't know, it's a bit confusing. I guess an official statement might clarify this, maybe you can post in their mailing list/forum/group/whatever. – Felix Kling Commented Feb 19, 2013 at 18:30 - 1 Why not store the selector seperately? – marsze Commented Feb 19, 2013 at 18:32
-
3
Uhm, in your use case, that's what
.filter
is for: api.jquery./filter.$test.filter(':checked').attr('id')
.$(':checked', $test)
cannot work, becauseinput
elements don't have any descendants. – Felix Kling Commented Feb 19, 2013 at 18:37 - 2 Yeah, well, we always learn something new :) – Felix Kling Commented Feb 19, 2013 at 18:39
3 Answers
Reset to default 4There should not be many reasons to actually need the original selector. In your specific use case, if you want to narrow down the set of selected elements, you can use .filter
[docs]:
$test.filter(':checked').attr('id')
$('#whatever').selector
still seems to work though. The documentation says "In 1.9, jQuery no longer attempts to maintain this property in chained methods [...]". Though http://api.jquery./selector claims it was removed in 1.9. I don't know, it's a bit confusing.
I would just save the selector in a variable like this:
var testSelector = 'input[name="test"]';
var test = $(testSelector);
console.log( $(testSelector + ':checked').attr('id') );
For the code I was working on I was able to replace
if ($elem.selector == 'date-column-filter')){
with
if ($elem.hasClass('date-column-filter')){
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744190263a4562401.html
评论列表(0条)