Put id in input hidden, working with jTemplate?
I'm working with jquery.ajax()
I'm getting a object and I use jTemplate to write the html. My problem is now that I need to place the id of the object in a input hidden. I have no idea how I should do this. I tried to do a <script>
in the template.htm with jquery to place the id in hidden but with no luck.
Any suggestions?
this is my jTemplate html file
<div style="background-color: #ccc">
{$T.Email}
</div>
<div style="background-color: #ddd">
{$T.Password}
</div>
This is my jquery
$('a').live('click', functio开发者_开发技巧n(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);
}
If you place the id in a JavaScript variable outside the function then you can access it in the template simply by putting {ItHere} in the template.
So do var id outside the function, and set it within the function.
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);
}
Then you can add the id in the template like this:
<img src="HelloWorld.jpg" id="{id}"/>
精彩评论