问题描述
我正在做一个简单的jQuery帖子:
I am doing a simple jQuery post:
$.post('/form.html',
$("#form").serialize(),
function(data, textStatus) {
//Selector for finding a field in the data
});
如何处理针对变量数据的选择器?
How do I process a selector against the variable data?
我需要查找html中存在的特定ID邮寄回来了吗?
I need to look for a specific id that exists within the htmlreturned from the post call?
推荐答案
数据是否为HTML块,其中包含带有id的HTML元素?
Is the data a block of HTML that has an HTML element with an id?
例如,假设帖子返回的数据如下
For example, say the data returned from the post looks like
"<div><p id='firstParagraph'>Some text</p><input type='hidden' id='hiddenField' value="42">Some more text</p></div>"
并说您只想提取"hiddenField"元素的值.你可以做
and say that you just want to pull out the value of the "hiddenField" element. You can do
$.post('/form.html',
$("#form").serialize(),
function(data, textStatus) {
var hiddenValue = $("#hiddenField", $(data) ).val();
//hiddenValue now equals "42"
});
选择器中的$(data)
部分正在创建一个上下文,选择器将在其中运行. $("#hiddenField", $(data) ).val()
与执行$(data).find("#hiddenField").val()
完全相同.
The $(data)
part in the selector is creating a context in which the selector will operate. $("#hiddenField", $(data) ).val()
is the exact equivalent of doing $(data).find("#hiddenField").val()
.
这篇关于jQuery-从.post返回的数据的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!