Javascript 如何在嵌套表td中显示警报值
我在一个循环中调用ajax,该循环位于内部,如下所示, 我能够在迭代循环时正确地警告值,我想要的是在Javascript 如何在嵌套表td中显示警报值,javascript,jquery,ajax,Javascript,Jquery,Ajax,我在一个循环中调用ajax,该循环位于内部,如下所示, 我能够在迭代循环时正确地警告值,我想要的是在部分显示相同的值,而不是发出警告,这可能吗 <!-- The template to display files available for download --> <script id="template-download" type="text/x-tmpl"> {% for (var i=0, file; file=o.files[i]; i++)
部分显示相同的值,而不是发出警告,这可能吗
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<!----- Ajax function call -------->
var a= file.name;
$.ajax({
type: "POST",
url: "http://localhost/myappproject/trips_controller/showStatus/",
dataType: "text",
data: {image_name: a,trip_id: '2'},
success: function(msg){
alert(msg);//how can i display this value in this place
}
});
</td>
</tr>
{% } %}
</script>
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
var a=file.name;
$.ajax({
类型:“POST”,
url:“http://localhost/myappproject/trips_controller/showStatus/",
数据类型:“文本”,
数据:{image_name:a,trip_id:'2'},
成功:函数(msg){
警报(msg);//如何在此处显示此值
}
});
{% } %}
关于上述代码中使用的x-templ
请写信给我,如果你不能理解我的问题,我会尝试用其他方式向你解释。从两天开始,我试了试,最后想问问那些每天都能看到StckOverflow的天才们:)–希望这能奏效:
$('#TableID').append('<tr><td>' + msg+ '</td></tr>');
$('#TableID')。追加(''+msg+'');
或
$(“”).append(“”+msg+“”);
编辑:我改变主意了,在阅读帖子后我修改了我的答案,你检查了吗
在html代码的顶部添加以下代码:
<script type="text/javascript">
function ajaxCall(fileName, index) {
$("#inner-file-msg-" + index).html("initializing...");
$.ajax({
type: "POST",
url: "http://localhost/myappproject/trips_controller/showStatus/",
dataType: "text",
data: {image_name: fileName,trip_id: '2'},
success: function(msg){
$("#inner-file-msg-" + index).html(msg);
}
});
}
</script>
函数ajaxCall(文件名、索引){
$(“#内部文件msg-”+索引).html(“初始化…”);
$.ajax({
类型:“POST”,
url:“http://localhost/myappproject/trips_controller/showStatus/",
数据类型:“文本”,
数据:{image\u name:fileName,trip\u id:'2'},
成功:函数(msg){
$(“#内部文件msg-”+索引).html(msg);
}
});
}
这是您的模板:
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<span id="inner-file-msg-{%=i%}"></span>
<script type="text/javascript">
ajaxCall("{%=file.name%}", "{%=i%}");
<\/script>
</td>
</tr>
{% } %}
</script>
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
ajaxCall(“{%=file.name%}”,“{%=i%}”);
{% } %}
Ps:我不确定这条线,如果它不起作用,你能把这条线改成吗?你可以这样试试:
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<span id="status-{%=i%}">loading...</span>
</td>
</tr>
//JavaScript function call- use x-templ syntax
showStatus(file.name,i);
{% } %}
</script>
<script>
function showStatus(name, id){
$.ajax({
type: "POST",
url: "http://localhost/myappproject/trips_controller/showStatus/",
dataType: "text",
data: {image_name: a,trip_id: '2'},
success: function(msg){
$('#status-'+id).html(msg);//or use native innerHTML prop
}
});
}
</script>
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<span id="file-{%=file.name%}-status"></span>
</td>
</tr>
<!----- Ajax function call -------->
(function (file) {
$.ajax({
// ...
success: function (msg){
$('#file-' + file.name + '-status').html(msg);
}
});
})(file);
{% } %}
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
加载。。。
//JavaScript函数调用-使用x-templ语法
showStatus(文件名,i);
{% } %}
函数showStatus(名称、id){
$.ajax({
类型:“POST”,
url:“http://localhost/myappproject/trips_controller/showStatus/",
数据类型:“文本”,
数据:{image_name:a,trip_id:'2'},
成功:函数(msg){
$('#status-'+id).html(msg);//或使用本机innerHTML属性
}
});
}
AJAX=异步JavasciptAndXml
试试这个
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<!----- Ajax function call -------->
var a= file.name;
$(document).ready(function (){ //run when page load is done
$.ajax({
type: "POST",
url: "http://localhost/myappproject/trips_controller/showStatus/",
dataType: "text",
data: {image_name: a,trip_id: '2'},
success: function(msg){
return '<span>' + msg + '</span>';
}
});
});
</td>
</tr>
{% } %}
</script>
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
var a=file.name;
$(document).ready(函数(){//在页面加载完成时运行
$.ajax({
类型:“POST”,
url:“http://localhost/myappproject/trips_controller/showStatus/",
数据类型:“文本”,
数据:{image_name:a,trip_id:'2'},
成功:函数(msg){
返回“”+消息+“”;
}
});
});
{% } %}
或使其同步
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<!----- Ajax function call -------->
var a= file.name;
$.ajax({
type: "POST",
url: "http://localhost/myappproject/trips_controller/showStatus/",
dataType: "text",
data: {image_name: a,trip_id: '2'},
success: function(msg){
return '<span>' + msg + '</span>';
},
async: false
});
</td>
</tr>
{% } %}
</script>
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
var a=file.name;
$.ajax({
类型:“POST”,
url:“http://localhost/myappproject/trips_controller/showStatus/",
数据类型:“文本”,
数据:{image_name:a,trip_id:'2'},
成功:函数(msg){
返回“”+消息+“”;
},
异步:false
});
{% } %}
小心,如果要使用此选项,请阻止/锁定所有其他进程,直到其完成。。。就像在PHP中没完没了地工作一样。所以如果你的请求没有结束。。这将导致一个小问题:)AJAX请求是异步的,因此它们的结果将在很久以后处理,而此时DOM已经构建好了。没有办法(除了使请求同步,我在这里不再进一步提及)像您试图做的那样“在调用请求的位置返回HTML”。这意味着根本没有理由将请求调用放在表单元格中。您只需要为表单元格分配一些标识符,以便以后能够找到它们并更改它们的内容。然后在某个地方执行AJAX调用。我不确定您正在使用的库的确切语法,但它看起来是这样的:
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<span id="status-{%=i%}">loading...</span>
</td>
</tr>
//JavaScript function call- use x-templ syntax
showStatus(file.name,i);
{% } %}
</script>
<script>
function showStatus(name, id){
$.ajax({
type: "POST",
url: "http://localhost/myappproject/trips_controller/showStatus/",
dataType: "text",
data: {image_name: a,trip_id: '2'},
success: function(msg){
$('#status-'+id).html(msg);//or use native innerHTML prop
}
});
}
</script>
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
<td>
<span>{%=file.name%}</span>
<span id="file-{%=file.name%}-status"></span>
</td>
</tr>
<!----- Ajax function call -------->
(function (file) {
$.ajax({
// ...
success: function (msg){
$('#file-' + file.name + '-status').html(msg);
}
});
})(file);
{% } %}
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
(功能(文件){
$.ajax({
// ...
成功:功能(msg){
$('#file-'+file.name+'-status').html(msg);
}
});
})(文件);
{% } %}
提醒一下——上面的示例是伪代码。您可能需要进行一些小的调整,以使其与您正在使用的库一起工作。您希望在何处显示值?可能有更好的方法,但是