[英]JavaScript escape string issue
I am defining onclick event to aa href based on a condition 我正在根据条件将onclick事件定义为aa href
Here's my script: 这是我的脚本:
<script>
function comment_pop(id)
{
alert(id);
$('#login_ret_url').val('<?php echo base_url();?>idea/view_idea/'+id+'?action=comment');
ShowModalPopup('div_login');
}
</script>
<div class="rate_image">
<a href="<?php if(isset($_SESSION['logged_user']))
echo base_url().'idea/view_idea/'.$row['idea_slug'].'?action=comment';
else
echo 'javascript:void(0)';?>"
<?php if(!isset($_SESSION['logged_user']))
echo "onclick='comment_pop(".$row['idea_slug'].");'";?>>
<?php echo $total_comment[$key][0]['total_cont'];?>
</a>
</div>
The problem rises in the line 问题在排队
echo "onclick='comment_pop(".$row['idea_slug'].");'";?>>
I am trying to concat the function with the variable so that I can send parameter to the JavaScript function but it's not working. 我正在尝试用变量来连接函数,以便可以将参数发送给JavaScript函数,但是它不起作用。
I think I need to use some escape parameters but I am really new to this. 我认为我需要使用一些转义参数,但我对此确实很陌生。
try this 尝试这个
echo "onclick='comment_pop(\'".$row['idea_slug']."\')'";?>>
or making it simple (separeting php and html code).. 或使其简单(分离php和html代码)。
<?php
if(isset($_SESSION['logged_user'])){
$hrefurl=base_url().'idea/view_idea/'.$row['idea_slug'].'?action=comment';
$onclickurl= "";
}else{
$hrefurl = 'javascript:void(0)';
$onclickurl= 'comment_pop("'.$row['idea_slug'].'");';
?>
<a href="<?php echo $hrefurl;?>" onclick="<?php echo $onclickurl; ?>" >
<?php echo $total_comment[$key][0]['total_cont'];?>
</a>
尝试转义“ onclick =“之后的单引号,例如
\'
Your current code echoes the idea_slug without quotes, ie it won't be a JS string (but a variable identifier, a number or something): 您当前的代码回显了没有引号的idea_slug,即它不是JS字符串(而是变量标识符,数字或其他内容):
…onclick='comment_pop(some_slug);'…
You want to wrap it in quotes presumably: 您可能想将其用引号引起来:
echo "onclick='comment_pop(\"".$row['idea_slug']."\");'";
so that you get 这样你就可以
…onclick='comment_pop("some_slug");'…
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.