当我将PHP变量作为参数

时间:2015-05-26 13:35:08

标签: php jquery

我有一个看起来像这样的jquery函数:

function removeSectionPhoto(image, section_id) {

    if(confirm("Are you sure you want to remove this image, doing so will also remove it from the Database.") == true) {

        $("#"+$(this).data("remove-row")).remove();
    }
    else{
        alert("Do nothing");
    }
}

该函数将需要参数,我从PHP输出<a>,其中包含一个onClick函数。

echo "<td><a href='#' class='removeImageRow' data-remove-row='fac_sec_photo_row". $imageRowCount . "' onClick='removeSectionPhoto()'>Remove</a></td>";

当我没有将任何参数传递给函数时,这工作正常,但是当我添加以下参数时,这样:

echo "<td><a href='#' class='removeImageRow' data-remove-row='fac_sec_photo_row". $imageRowCount . "' onClick='removeSectionPhoto(". $sectionPhotoRow['photo'] .", ". $facilitySecId .")'>Remove</a></td>";

似乎打破了触发功能。我检查了我的浏览器控制台,单击添加参数的按钮后,我收到语法错误消息

  

Uncaught SyntaxError:意外的令牌。

我是否错误地将这些参数连接起来,或者......?

如何修复它以便我的函数将使用添加的参数触发,而不会让我回到语法错误?

2 个答案:

答案 0 :(得分:1)

您应该在引号中添加参数:

echo "<td><a href='#' class='removeImageRow' data-remove-row='fac_sec_photo_row". $imageRowCount . "' onClick='removeSectionPhoto(\"". $sectionPhotoRow['photo'] ."\", \"". $facilitySecId ."\")'>Remove</a></td>";

更改内容:

onClick='removeSectionPhoto(\"". $sectionPhotoRow['photo'] ."\", \"". $facilitySecId ."\")'

答案 1 :(得分:0)

尝试使用此

echo "<td><a href='#' class='removeImageRow' data-remove-row='fac_sec_photo_row". $imageRowCount . "' onClick='removeSectionPhoto(\"". $sectionPhotoRow['photo'] ."\", \"". $facilitySecId ."\")'>Remove</a></td>";