简体   繁体   中英

Uncaught Syntax error on onClick function javascript

I need to output some strings in the onclick anchor function.I have used proper escaping but anyhow its returning error.

My code is:

$output .= '<a title="Minus this" href="#" onclick = removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');></a>';

And also used this :

$output .= '<a title="Minus this" href="#" onclick = "removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ')"></a>';

But in both cases there is Uncaught SyntaxError: Unexpected token }

您应该使用单引号来表示HTML元素中的字符串

$output .= '<a title="Minus this" href="#" onclick = "removefromCart(\'' . $item . '\', \'' . $nonce . '\', ' . $deductQty . ')"></a>';

The quotes is totally wrong. Do this way, using the first one:

$output .= '<a title="Minus this" href="#" onclick=\'removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');\'></a>';

See the ' s I have added and removed spaces? And please, next time, don't mingle both PHP and JavaScript. It's confusingly dangerous.

See also:

For greater than or equal to PHP5

$output .= '<a title="Minus this" href="#" onclick = removefromCart($item,$nonce,$deductQty);></a>';

For Less than PHP5 try it

$output .= '<a title="Minus this" href="#" onclick = removefromCart(' . $item . ',' . $nonce . ', ' . $deductQty . ');></a>';

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.

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