我正在使用jquery.ajax(),我正在获取一个对象,并且我使用jTemplate来编写html。我现在的问题是,我需要将对象的id放在一个隐藏的输入中。我不知道我该怎么做。我试着用jquery在template.htm中做一个<script>,把id隐藏起来,但是没有成功。
有什么建议吗?
这是我的jTemplate html文件
<div style="background-color: #ccc">
{$T.Email}
</div>
<div style="background-color: #ddd">
{$T.Password}
</div>这是我的jquery
$('a').live('click', function(evt) {
evt.preventDefault();
var id = $(this).attr('id');
$.ajax({
type: "POST",
url: "GetUserWeb.asmx/GetUser",
data: "{'value': '" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
ApplyTemplate(msg);
},
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
});
function ApplyTemplate(msg) {
$('#Container').setTemplateURL('template.htm');
$('#Container').processTemplate(msg.d);
}发布于 2011-03-02 01:13:24
如果将id放在函数外部的JavaScript变量中,则只需将{ItHere}放入模板中即可在模板中访问它。
所以在函数外部执行var id,并在函数内设置它。
var id;
$('a').live('click', function(evt) {
evt.preventDefault();
id = $(this).attr('id');
$.ajax({
type: "POST",
url: "GetUserWeb.asmx/GetUser",
data: "{'value': '" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
ApplyTemplate(msg);
},
error: function(xhr, err) {
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
alert("responseText: " + xhr.responseText);
}
});
});
function ApplyTemplate(msg) {
$('#Container').setTemplateURL('template.htm');
$('#Container').processTemplate(msg.d);
}然后您可以在模板中添加id,如下所示:
<img src="HelloWorld.jpg" id="{id}"/>https://stackoverflow.com/questions/2736228
复制相似问题