This might sound a little dumb, but can you please point out why in the following script 'firstValue' is undefined and 'secondValue' is as needed, i.e. 4.
这可能听起来有点愚蠢,但是请你指出为什么在下面的脚本中'firstValue'是未定义的,'secondValue'是根据需要,即4。
<div >
<input class="feedback-selected" type="hidden" value="4" />
</div>
<script type="text/javascript">
var firstValue = $(this).find('.feedback-selected').val();
var secondValue = $('.feedback-selected').val();
alert(firstValue);
alert(secondValue);
</script>
I am sure I am not using the find function as it should be.
我确信我没有使用find函数。
6 个解决方案
#1
this doesn't have a value in your example, so the find() function will not find anything.
这在您的示例中没有值,因此find()函数将找不到任何内容。
(this is only valid in instance functions or event handlers)
(这仅在实例函数或事件处理程序中有效)
#3
What activa said... and you could probably use $(document) instead of $(this) if you need to use .find()
什么激活说...如果你需要使用.find()你可以使用$(document)而不是$(this)
#4
activa's answer is correct. $("<selector>")
is the equivalent of what you were trying to do with the first search. find("<selector>")
is meant for searching within a wrapped set, not as a starting point.
activa的回答是正确的。 $(“
However, I would add that a better way of retrieving the value in this case is:
但是,我想补充一点,在这种情况下检索值的更好方法是:
var value = $("input.feedback-selected").val();
This is because including the type of the element will allow jQuery to search the DOM in a quicker way.
这是因为包含元素的类型将允许jQuery以更快的方式搜索DOM。
#5
find() searches for descendant elements of the jQuery object you call it on. You call find() on the results of $(this), which is nothing. If you want to call find() on the document, you'll have to use $(document).
find()搜索您调用它的jQuery对象的后代元素。你在$(this)的结果上调用find(),这没什么。如果要在文档上调用find(),则必须使用$(document)。
#6
this
in your example is not a DOM element that contains any subelements of class "feedback-selected", so the find function can't find them. It might not even be a DOM element.
在您的示例中,这不是包含“反馈选择”类的子元素的DOM元素,因此find函数无法找到它们。它甚至可能不是DOM元素。
I would note that $(this) can be valid anywhere this points to a DOM element. So if pretty much anytime you're triggering a function that is called from a DOM element, you're fine. It's all about the scope in this case. This would be valid too:
我会注意到$(this)在指向DOM元素的任何地方都是有效的。因此,如果您在触发从DOM元素调用的函数时,几乎任何时候都可以。这就是本案的范围。这也是有效的:
function aha() {
alert($(this).find('.feedback-selected').val());
}
aha.apply(document);
#1
this doesn't have a value in your example, so the find() function will not find anything.
这在您的示例中没有值,因此find()函数将找不到任何内容。
(this is only valid in instance functions or event handlers)
(这仅在实例函数或事件处理程序中有效)
#2
Explanation of what 'this' means in jQuery
解释jQuery中“this”的含义
#3
What activa said... and you could probably use $(document) instead of $(this) if you need to use .find()
什么激活说...如果你需要使用.find()你可以使用$(document)而不是$(this)
#4
activa's answer is correct. $("<selector>")
is the equivalent of what you were trying to do with the first search. find("<selector>")
is meant for searching within a wrapped set, not as a starting point.
activa的回答是正确的。 $(“
However, I would add that a better way of retrieving the value in this case is:
但是,我想补充一点,在这种情况下检索值的更好方法是:
var value = $("input.feedback-selected").val();
This is because including the type of the element will allow jQuery to search the DOM in a quicker way.
这是因为包含元素的类型将允许jQuery以更快的方式搜索DOM。
#5
find() searches for descendant elements of the jQuery object you call it on. You call find() on the results of $(this), which is nothing. If you want to call find() on the document, you'll have to use $(document).
find()搜索您调用它的jQuery对象的后代元素。你在$(this)的结果上调用find(),这没什么。如果要在文档上调用find(),则必须使用$(document)。
#6
this
in your example is not a DOM element that contains any subelements of class "feedback-selected", so the find function can't find them. It might not even be a DOM element.
在您的示例中,这不是包含“反馈选择”类的子元素的DOM元素,因此find函数无法找到它们。它甚至可能不是DOM元素。
I would note that $(this) can be valid anywhere this points to a DOM element. So if pretty much anytime you're triggering a function that is called from a DOM element, you're fine. It's all about the scope in this case. This would be valid too:
我会注意到$(this)在指向DOM元素的任何地方都是有效的。因此,如果您在触发从DOM元素调用的函数时,几乎任何时候都可以。这就是本案的范围。这也是有效的:
function aha() {
alert($(this).find('.feedback-selected').val());
}
aha.apply(document);