将php函数传递给jquery

时间:2011-10-26 08:53:14

标签: php jquery variables

考虑以下Jquery外部脚本片段,用于在模态窗口中创建youtube iframe:

$(function() {

    $(document).ready(function() {  

    var myPhpVa = '<?php echo embeddable($ytid);?>';

    $('a[name="modal"]').click(function(e) {

        $('<iframe width="560" height="315" src=myPhpVa frameborder="0" allowfullscreen>').appendTo('#dcontent');

    }

});

出于某种原因,我找不到对象。 PHP函数在此javascript之上声明,声明为:

function embeddable($ytid){
        return $embeddable = 'http://www.youtube.com/embed/'.$ytid;
    }

非常感谢任何传递此PHP变量的帮助。

3 个答案:

答案 0 :(得分:3)

首先 - 分配返回值是没用的。删除它:

function embeddable($ytid){
    return 'http://www.youtube.com/embed/'.$ytid;
}

关于$(document).ready(function() {$(function() {的相同内容 - 它们相等且加倍 - 删除其中一个。

指定myPhpVa变量后 - 写

alert(myPhpVa);

并查看它显示的内容。

答案 1 :(得分:2)

我认为将iframe附加到html的代码语法不正确。

    $('<iframe width="560" height="315" src="' + myPhpVa + '" frameborder="0" allowfullscreen>').appendTo('#dcontent');

答案 2 :(得分:1)

看起来你没有突破字符串以将myPhpVa连接到js文字中。试试这个:

$('<iframe width="560" height="315" src="' +  myPhpVa + '" frameborder="0" allowfullscreen>').appendTo('#dcontent'); 

如果这不起作用,请提醒myPhpVa的值,看看它是否符合预期。