如何在ajax链接函数中传递js变量

时间:2012-12-24 07:15:48

标签: php javascript yii

如何在网址中传递studentID?因为它的js变量得到错误或者它作为字符串传递。

<script>    
    function updateCallhistory(studentID)
    {  
        <?php   
            echo CHtml::ajax(array(
            'url'=> Yii::app()->createUrl("siteiq/UpdateStudentForm", array("ClassID" => $ClassID) ),           
            'data'=> "js:$(this).serialize()",
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
            if (data.status == 'failure')
            {
                $('#dialogStudentForm div.divForForm').html(data.div);
                  // Here is the trick: on submit-> once again this function!
                $('#dialogStudentForm div.divForForm form').submit(updateCallhistory);
            }
            else
            {
                $('#dialogStudentForm div.divForForm').html(data.div);
                setTimeout(\"$('#dialogStudentForm').dialog('close') \",1000);
            }

            } ",
        ))?>;
        return false;  
    }
    </script>

1 个答案:

答案 0 :(得分:2)

尝试这种方式:

<script>    
    function updateCallhistory(studentID)
    {  

        var url = '<?php echo Yii::app()->createUrl("siteiq/UpdateStudentForm", array("ClassID" => $ClassID) ); ?>' + '&studentID='+studentID;
        <?php   
            echo CHtml::ajax(array(
            'url'=> 'js:url',           
            'data'=> "js:$(this).serialize()",
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
            if (data.status == 'failure')
            {
                $('#dialogStudentForm div.divForForm').html(data.div);
                  // Here is the trick: on submit-> once again this function!
                $('#dialogStudentForm div.divForForm form').submit(updateCallhistory);
            }
            else
            {
                $('#dialogStudentForm div.divForForm').html(data.div);
                setTimeout(\"$('#dialogStudentForm').dialog('close') \",1000);
            }

            } ",
        ))?>;
        return false;  
    }
    </script>