I want to send two php values to a javascript function. Apparently it doesn't do anything. Is my syntax correct?
<script>
function onmouseclick(nr, name){
alert(nr);
alert(name);
}
</script>
<?php
echo '<img onClick="javascript: onmouseclick('. $template->id .','. $template->name .');" "class="img" />';
?>
您应该添加引号:
echo '<img onClick="javascript: onmouseclick("'. $template->id .'", "'. $template->name .'");"
Yes you need to use as
echo '<img onClick="javascript: onmouseclick(\''. $template->id .'\',\''. $template->id .'\');" class="img" />';
Need to add the param inside quotes ' ' but since you are doing echo '...'
you need to use escape \\ so that the quotes are treated as string.
just use without php tags
<img onClick="javascript:onmouseclick('<?php echo $template->id;?>','<?php echo $template->name;?>');" class="img" />
or with php tags
echo '<img onClick="javascript: onmouseclick("'. $template->id .'", "'. $template->name .'");"
您可以使用它来避免字符逃脱
echo sprintf('<img onclick="%s" class="img"/>',sprintf("onmouseclick(%d,'%s');",$template->id,$template->id));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.