Javascript 无法将数据追加到Div并重定向页面

Javascript 无法将数据追加到Div并重定向页面,javascript,jquery,html,append,Javascript,Jquery,Html,Append,我有一个带有项目列表的默认页面。当我点击这些项目时,我需要动态地将数据附加到B页的div中,并将应用重定向到B页 我在PageB中添加了这个div '' 单击事件时,我正在.js文件中执行以下操作: '$(document).on('click', '#selectConcept', function (node) { var ncid = this.textContent.slice(6,25); $.ajax({ dataType: "json",

我有一个带有项目列表的默认页面。当我点击这些项目时,我需要动态地将数据附加到B页的div中,并将应用重定向到B页

我在PageB中添加了这个div ''

单击事件时,我正在.js文件中执行以下操作:

'$(document).on('click', '#selectConcept', function (node) {
    var ncid = this.textContent.slice(6,25);
    $.ajax({

        dataType: "json",

        url: "http://txv-trmindexer01:8080/CommonTerminologyLeopardSearch/rest/getConceptByNcid/" + ncid,

        error: function () {
            alert("ERROR");
        },
        success: function (data) {
            window.location.href = 'getfacets.html';
            for (var result = 0; result < finalSearchResults.length; result++) {
                if (finalSearchResults[result].ncid == ncid) {

                    $("#selectedConceptitem").empty();
                    var selectedconcept = "<p>" + "ncid: " + finalSearchResults[result].ncid + "," + "cid: " + finalSearchResults[result].cid + "</p>";

                    $(selectedconcept).appendTo("#selectedConceptitem");


                }
            }

        }    });
});'
”$(文档)。在('click','selectConcept',函数(节点){
var ncid=this.textContent.slice(6,25);
$.ajax({
数据类型:“json”,
url:“http://txv-trmindexer01:8080/CommonTerminologyLeopardSearch/rest/getConceptByNcid/“+ncid,
错误:函数(){
警报(“错误”);
},
成功:功能(数据){
window.location.href='getfacets.html';
对于(var result=0;result”;
$(selectedconcept)。附加到(“#selectedConceptitem”);
}
}
}    });
});'
我可以重定向页面,但没有向Div追加任何内容


有人能帮我解决这个问题吗?

我不太确定,但我想代码在加载新页面之前运行。因此,您可以尝试将代码包装到在
onload
事件时间运行的函数中

window.location.href='getfacets.html';
window.onload=函数(){
对于(var result=0;result”;
$(selectedconcept)。附加到(“#selectedConceptitem”);
}
}
}

问题:

一旦您设置了“window.location.href”属性,页面就会导航到您的页面B,您就会丢失所获取的数据

这个问题有两种解决方案:

  • 使用单页应用程序(SPA)应用程序方法,您可以为所获取的数据创建一个新的全局范围,该范围现在可由页面B使用

  • 将ncID作为查询字符串参数发送到B页,并在B页上实现服务调用和数据附加逻辑