我想将下面代码的append()中的html内容移动到新的.html文件,然后使用ajax将这些内容追加到我的文件中
这是我的旧代码
for(var i = 0; i < data.length; i++) {
$('#table_body').append('<tr><td>'+ data[i].variable1 +'</td><td><a href="javascript:void(0)" onclick="callFunctionA(\''+data[i].variable2+'\');"></a></td></tr>');
}
我想将我的代码改为像这样
.js文件:
for(var i = 0; i < data.length; i++) {
$.ajax({
url: "table_body.html",
success : function(result){ $('#table_body').append(result) }
.html文件:
<tr>
<td>data[i].variable1</td>
<td>
<a href="javascript:void(0)" onclick="callFunctionA(\''+data[i].variable2+'\');"></a>
</td>
</tr>
但是如何将动态值传递给data [i] .variable1和data [i] .variable2?
提前致谢。
答案 0 :(得分:0)
编辑:刚刚提出了与原始解决方案不同的解决方案。假设您不想在另一侧获取GET参数。现在都是js。
reshapeLong2 <- function(data, varying = NULL, timevar = "time", idvar = "id", sep = ".", patterns = NULL) {
require(plyr)
substrRight <- function(x, n){
substr(x, nchar(x)-n+1, nchar(x))
}
if (is.null(varying))
varying <- names(data)[! names(data) %in% idvar]
# Create pattern if not specified, guesses by taking numbers given at end of variable names.
if (is.null(patterns)) {
times <- unique(na.omit(as.numeric(substrRight(varying, 1))))
times <- times[order = times]
patterns <- paste0(sep, times)
}
# Create list of datasets by study time
ls.df <- lapply(patterns, function(pattern) {
var.old <- grep(pattern, x = varying, value = TRUE)
var.new <- gsub(pattern, "", x = var.old)
df <- data[, c(idvar, var.old)]
names(df) <- c(idvar, var.new)
df[, timevar] <- match(pattern, patterns)
return(df)
})
# Concatenate datasets together
dfs <- rbind.fill(ls.df)
return(dfs)
}
> reshapeLong2(df.test)
id inlove mood time income
1 1 FALSE sad 1 NA
2 2 TRUE so-so 1 NA
3 3 TRUE sad 1 NA
4 1 TRUE <NA> 2 27766.13
5 2 FALSE <NA> 2 74395.30
6 3 TRUE <NA> 2 89004.95
7 1 NA sad 3 27270.07
8 2 NA so-so 3 36971.64
9 3 NA so-so 3 85986.96
Warning message:
In na.omit(as.numeric(substrRight(varying, 1))) :
NAs introduced by coercion
并在<script>
var data = [{variable1:'row1 td1',variable2:'row1 td2'},{variable1:'row2 td1',variable2:'row2 td2'}];
for(var i = 0; i < data.length; i++) {
$.ajax({
url: "table_body.html",
indexVal: i,
success: function(result,i){
id = this.indexVal;
res = result.replace("__data1__", data[id].variable1).replace("__data2__", data[id].variable2);
$('#table_body').append(res);
}
});
}
</script>
文件中
table_body.html