Javascript 如何在fancybox函数的ajax调用中将jquery变量作为url参数传递
我正在使用jquery和SpringMVC在应用程序中使用$.fancybox()函数。我有下面的函数,我正试图使用fancybox打开一个模型窗口,将一些数据显示为弹出窗口Javascript 如何在fancybox函数的ajax调用中将jquery变量作为url参数传递,javascript,jquery,ajax,Javascript,Jquery,Ajax,我正在使用jquery和SpringMVC在应用程序中使用$.fancybox()函数。我有下面的函数,我正试图使用fancybox打开一个模型窗口,将一些数据显示为弹出窗口 $(document).on('click', '#versionBtn' ,function (event){ $(this).toggleClass('selected'); webContentId = $(".selected").closest("div").attr("id"); con
$(document).on('click', '#versionBtn' ,function (event){
$(this).toggleClass('selected');
webContentId = $(".selected").closest("div").attr("id");
console.log('The selected webContentId : '+webContentId );
$(this).toggleClass('selected');
$.fancybox({
type: 'ajax',
href : '${rc.getContextPath()}/module/simplewebcontent/list-webcontent-versions?webContentDefinitionId=webContentId',
autoSize : false,
closeClick : false,
closeBtn : true,
fitToView : true,
autoCenter : true
});
});
现在,当调用click函数时,url中没有设置变量webContentId
的值。
是否有任何方法可以在url中获取该变量的值…您只需将该值添加(连接)到地址字符串,它将作为get请求发送到服务器
href : '${rc.getContextPath()}/module/simplewebcontent/list-webcontent-versions?webContentDefinitionId=' + webContentId,
试着这样做:
$.fancybox({
type: 'ajax',
href : '${rc.getContextPath()}/module/simplewebcontent/list-webcontent-versions?webContentDefinitionId='+webContentId,
autoSize : false,
closeClick : false,
closeBtn : true,
fitToView : true,
autoCenter : true
});
您希望在url中传递javascript变量webContentId的值,而不是字符串“webContentId”
祝你好运基本解决方案与其他答案相同,但我更喜欢这种方法,尤其是多参数:
$.fancybox({
type: 'ajax',
href: '${rc.getContextPath()}/module/simplewebcontent/list-webcontent-versions?' + $.param({
webContentDefinitionId: webContentId
}),
autoSize : false,
closeClick : false,
closeBtn : true,
fitToView : true,
autoCenter : true
});
如果需要,jQueryparam
函数会为您执行必要的转义(现在,情况并非如此)