Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在fancybox函数的ajax调用中将jquery变量作为url参数传递_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 如何在fancybox函数的ajax调用中将jquery变量作为url参数传递

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

我正在使用jquery和SpringMVC在应用程序中使用$.fancybox()函数。我有下面的函数,我正试图使用fancybox打开一个模型窗口,将一些数据显示为弹出窗口

$(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
});
如果需要,jQuery
param
函数会为您执行必要的转义(现在,情况并非如此)