简体   繁体   English

从PHP echo调用带有特定变量的JS函数的问题

[英]Issue calling JS function from PHP echo with a specific variable

I have a JS function that will add keys with values to the current URL. 我有一个JS函数,它将带有值的键添加到当前URL。 There are two almost identical links below, only difference is the variable being passed to the JS function. 下面有两个几乎完全相同的链接,唯一的不同是将变量传递给JS函数。 One link passes $month, the other passes $event_category. 一个链接通过$ month,另一个链接通过$ event_category。 For some reason, when passing $event_category, the JS function doesn't even get called. 出于某种原因,当传递$ event_category时,甚至不会调用JS函数。 Anyone know what I'm doing wrong? 有人知道我在做什么错吗?

You'll have to scroll to the right to see where the difference is. 您必须向右滚动才能看到不同之处。

    $month = 1;
    $event_category = (string) ($eventCategories[$k]["event_category"]);

    echo gettype($event_category); // prints "string"


    // doesn't work?
    echo '<div class="month selected"><a href="javascript:void(0);" onclick="javascript:insertParam('. "'event_category'" .', '. $event_category .');" class="button" role="button">
    <image width="100" height="60" src="images/'. $images_list[$eventCategories[$k]["event_category"]].'"></a></div>';  

    // works
    echo '<div class="month selected"><a href="javascript:void(0);" onclick="javascript:insertParam('. "'event_category'" .', '. $month .');" class="button" role="button">
    <image width="100" height="60" src="images/'. $images_list[$eventCategories[$k]["event_category"]].'"></a></div>';      

You should put quotes around the $event_category, otherwise it will be interpreted by javascript as a variable. 您应该在$ event_category前后加上引号,否则javascript将其解释为变量。 So, convert 因此,转换

. $event_category .

to

. '"' . $event_category . '"' .

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

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