简体   繁体   English

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

[英]How to properly concatenate values coming from Javascript and 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. 如您所见, id是来自Javascript的值,而$value是来自PHP的$value 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. 然后,我需要在定义之后立即将href var用作<a>元素的一部分。

With my code above I am getting this error: 在上面的代码中,我收到此错误:

Uncaught SyntaxError: Invalid or unexpected token 未捕获的SyntaxError:无效或意外的令牌

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. 注意函数$.extend如何正确关闭。 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 . console.log(id)确实打印了options.rowId的值,但是此值对超链接没有任何影响,因为您可能会注意到,这是/sf/distributor/show/ + options.rowId

What is coming in $value is a plain string in the case above /sf/distributor/show/ . /sf/distributor/show/上面的情况下, $value包含一个普通字符串。

You're missing double quotes and to end and reopen the string around options.rowId and a + in: 您丢失了双引号,并以结尾结尾并重新打开options.rowId+中的字符串:

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

It should be: 它应该是:

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

在这里,您缺少“”,因此您将options.rowId添加为字符串文本。

href='/sf/distributor/show/" + options.rowId + "' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM