dongxu198714 2017-05-31 14:30
浏览 29
已采纳

如何正确连接来自Javascript和PHP的值?

I have been struggling with this for the last two hours and I can't get it to work. Take a look to the following piece of code:

$js = '$.extend($.fn.fmatter , {
            userActions : function(cellvalue, options, rowData, addOrEdit) {
            var data = cellvalue.split("|");
            var id = options.rowId;
            var actions = "";';

foreach ($editorActions as $linkType => $value) {
    switch ($linkType) {
        case 'view':
            $js .= "if(data[1] == 1) {
                        actions += \"<a class='actionimage' href='" . $value . " + options.rowId' title='" . $this->translate->_e('View') . "' onClick='load_start();'><img src='/images/icons/16x16/document_view.png' width='16' height='16' alt='' /></a>\";
                    }";
    }

    break;
}

As you can see id is a value coming from Javascript and $value is coming from PHP. The idea is to get an hyperlink as for example:

$value = "/route/to/function/";
id = 19009; // I've omitted the $ sign since this var is coming from JS

var href = "' . $value . '" + id;'

Then I need to use the href var as part of the <a> element shown right after the definition.

With my code above I am getting this error:

Uncaught SyntaxError: Invalid or unexpected token

Can I get some help to get this right?

UPDATE:

This is how the code looks like after I render the page:

$(function () {
    $.extend($.fn.fmatter, {
        userActions: function (cellvalue, options, rowdata) {
            var data = cellvalue.split('|');
            var id = options.rowId;
            var actions = '';

            console.log(id);
            if (data[1] == 1) {
                actions += "<a class='actionimage' href='/sf/distributor/show/ + options.rowId' title='View' onClick='load_start();'><img src='/images/icons/16x16/document_view.png' width='16' height='16' alt='' /></a>";
            }

            return actions;
        }
    });
});

Notice how the function $.extend close properly. console.log(id) did print the value of options.rowId however this value doesn't have any effect on the hyperlink as you may notice this is the value /sf/distributor/show/ + options.rowId.

What is coming in $value is a plain string in the case above /sf/distributor/show/.

  • 写回答

2条回答 默认 最新

  • doulouli8686 2017-05-31 14:52
    关注

    You're missing double quotes and to end and reopen the string around options.rowId and a + in:

    actions += \"<a class='actionimage' href='" . $value . " + options.rowId'
    

    It should be:

    actions += \"<a class='actionimage' href='" . $value . "\" + options.rowId + \"'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元