JQuery basics - Selecting elements inside a cached element
Sorry but this is a really basic jQuery syntax question, but I can't find anyone discussing it anywhere (probably as I don't know the correct terms to search for).
I want to select/cache a using a variable and then later get the value of checked inputs within that . Now I know I could do this without caching as:
var questionID = (a string)
$(questionID + " input:radio:checked").val()
But I use the div#'questionID' object a number of times so want to cache it i.e.
questionID = '#' + ...Something that changes ...
$question = $(questionID);
Now that $question is a jQuery object I can't work out how to select things inside it (without using children())
For example the following don't work:
$question.(' input:radio:checked)
$($question 'input:radio:checked')
I imagine this is a really basic bit of syntax, but I can't find it anywhere and I've tried lots of combination开发者_Python百科s with no luck......
Any help would be highly appreciated and apologies if this is really dumb but I'm very new to jQuery,
Nick
You can use
var checkedRadios = $('input:radio:checked', $question);
OR
var checkedRadios = $question.find('input:radio:checked');
This is called tree traversing, you can find more about tree traversing api in jQuery Documentation
The find method is used to search for elements under a given scope.
Use .find(), I think thats what your looking for!
$question.find('input:radio:checked');
EDIT I suggest skimming through jQuery API, http://api.jquery.com/category/traversing/tree-traversal/
I think your looking for
var checkedRadiosInsideQuestion = $question.find('input:radio:checked');
Try $('input:radio:checked', $question)
...
精彩评论