简体   繁体   中英

jquery:SyntaxError: unterminated string literal

when ever i am giving a space between a word & i click to call a function, then i am getting the below error:

SyntaxError: unterminated string literal
[Break On This Error]   

$(this).SelectProjectBox(363,"ssss

mypage# (line 1, col 29)

Browser generated html:

<span ssasas")="" onclick="$(this).SelectProjectBox(363,"ssss" href="#">ssss ssasas</span>

My actual code

  echo '<span href="#"  onClick=$(this).SelectProjectBox(' . $cat->project_id . ',"'.$cat->project_name.'")>' . $cat->project_name . '</span>';

what is the cause Please suggestion, how can i modify my php code for this?

you missing quotes on onclick event try

echo '<span href="#"  onClick="$(this).SelectProjectBox(' . $cat->project_id . ','.$cat->project_name.')">' . $cat->project_name . '</span>';

or try

<span href="#"  onClick="$(this).SelectProjectBox(<?php echo $cat->project_id ;?>,<?php echo $cat->project_name;?>)"><?php echo $cat->project_name;?></span>

If I may give you some advice: split up your code a bit more. It is easier for people to understand:

$content  = "<span href='#' ";  
$content .= "onClick=\"$(this).SelectProjectBox($cat->project_id,'$cat->project_name')\"";
$content .= ">";
$content .= $cat->project_name . "</span>";

echo $content;

Escape single quote( ' ) using \\ .

\\' will echo '

To specify a literal single quote, escape it with a backslash ( \\ ). To specify a literal backslash, double it ( \\\\ ). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \\r or \\n , will be output literally as specified rather than having any special meaning.

Code

echo '<span href="#"  onClick="$(this).SelectProjectBox(\''.$cat->project_id .'\',\''.$cat->project_name.'\')">'.$cat->project_name.'</span>';

Read String

Instead of

echo '<span href="#"  onClick=$(this).SelectProjectBox(' . $cat->project_id . ',"'.$cat->project_name.'")>' . $cat->project_name . '</span>';

try with

echo '<span href="#"  onClick="$(this).SelectProjectBox(\''.$cat->project_id .'\',\''.$cat->project_name.'\')">'.$cat->project_name.'</span>';

Here, we are echoing the html part and we are using single quotes for enclosing the entire html string. So whenever we want single quotes in the string , here we want it for functions, we have to escape it using backslash as /' which will display a single quote. Otherwise the string will get stopped at that point.

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