[英]jquery Uncaught SyntaxError: Invalid or unexpected token - Input text has an apostrophe
I have the current problem regarding an input which I use for searching a vast variety of articles.我目前遇到有关用于搜索各种文章的输入的问题。 To get more in depth, the thing is that when I insert a text with an apostrophe, I get the following error: Uncaught SyntaxError: Invalid or unexpected token
为了更深入,问题是当我插入带有撇号的文本时,我收到以下错误: Uncaught SyntaxError: Invalid or unexpected token
错误Uncaught SyntaxError: Invalid or unexpected token
function set_id_article_popover(id, name){
$('#articles_search_input').val(id);
$('#articles_search_popover').val(name);
$('#articles_search_popover').blur();
$('[data-toggle="popover"]').each(function () {
$(this).popover('hide');
});
$('#articles_search_popover').val(name);
is where I insert article's name.是我插入文章名称的地方。 $('#articles_search_input').val(id);
gets the id of the article to search.获取要搜索的文章的 id。 First number is inserted correctly inside the function, which is the id of the article set_id_article_popover(id, name)
函数内部正确插入第一个数字,即文章的id set_id_article_popover(id, name)
<div class="item" id="articles_search_popover_container">
<i class="fa fa-newspaper-o placeholder_input_icotext-muted_light"></i>
<input name='name_busqueda'
autocomplete ='off' <?php if(!empty($value1))
{echo 'value="'.$value1.'"';} ?> type="text"
data-texts='1'
data-toggle="popover"
title="<i class='fa fa-exclamation-triangle'></i> <span class='text-muted'><?php echo __('Busca por especialidad o tratamiento');?></span>" data-lang="<?php echo $this->params['language'];?>"
data-search="articles"
id="articles_search_popover"
data-lang="<?php echo $lang; ?>"
class="form-control"
placeholder="<?php echo __('Buscar en artículos');?>">
<!-- Popover "Especialidad / Especialista" -->
<?php echo $this->element(
'/main_search/custom_autocomplete',
array(
'id' => 'articles_search_popover',
'headers' => false,
'specialties' => true,
'specialist'=> false,
'top_specialties' => false,
'treatments' => true,
'cols' => '1')
); ?>
</div>
Now, I will attach you the part of the Controller to have a better grasp of what is happening in the back-end.现在,我将附上 Controller 的部分,以便更好地了解后端发生的情况。
if(isset($this->request->query['name_busqueda'])
&& $this->request->query['name_busqueda'] != ""){
$word = $this->request->query['name_busqueda'];
}
if(isset($word) && $word != "" && !strpos($word, "-")){
$words_titulo = $word;
}
if(isset($articles_search) && $articles_search != ""){
$word_search = false;
//specialty or treatment is selected
$data = $this->request->query['articles_search'];
$data = explode("_",$data);
$tipo = $data[0];
if($tipo === 'specialty'){
//search by specalty
$specialty_id = $data[1];//id
if(!empty($this->params['language']) && $this->params['language'] != 'es'){
$name_field_sufix = '_' . $this->params['language'];
} else {
$name_field_sufix = '';
}
$specialty = $this->Specialization->find('first', array(
'fields' => array(
'name' . $name_field_sufix .' AS name'
),
'conditions' => array(
'id' => $specialty_id
)
));
if(!empty($specialty)) {
$words_titulo = $specialty['Specialization']['name'];
}
$options['conditions'] = array(
'ArticlesSpec.specialization_id' => $specialty_id
);
$this->set('specialty_id', $specialty_id);
}else{
//search by treatment
$treatment_id = $data[1];//id
if(!empty($this->params['language']) && $this->params['language'] != 'es'){
$name_field_sufix = '_' . $this->params['language'];
} else {
$name_field_sufix = '';
}
$treatment = $this->Treatment->find('first', array(
'fields' => array(
'name' . $name_field_sufix .' AS name'
),
'conditions' => array(
'id' => $treatment_id
)
));
$aux_join = array(
array(
'table' => 'td_articles_treatments',
'alias' => 'ArticlesTreat',
'type' => 'LEFT',
'conditions' => array(
'ArticlesTreat.article_id = Article.id',
)
),
array(
'table' => 'td_treatments',
'alias' => 'Treatments',
'type' => 'LEFT',
'conditions' => array(
'ArticlesTreat.treatment_id = Treatments.id',
),
)
);
$options['joins'] = array_merge($joins, $aux_join);
$campos_aux = ', ArticlesTreat.*, Treatments.*';
$options['conditions'] = array('ArticlesTreat.treatment_id' => $treatment_id);
if(!empty($treatment)) {
$words_titulo = $treatment['Treatment']['name'];
}
}
}
Do you know if there is a possibility of transforming the apostrophe through Cake or would it be better to do it through Javascript ?你知道是否有可能通过 Cake 来转换撇号,还是通过 Javascript 来做会更好?
I tried applying some solutions by replacing the apostrophe with an escape plus an alternative encoding character so that the input would be read more concisely, but I am kind of at a dead-end here.我尝试通过用转义符和替代编码字符替换撇号来应用一些解决方案,以便更简洁地读取输入,但我在这里陷入了死胡同。 Any help would be quite appreciated.任何帮助将不胜感激。 I am attaching you a couple of screen captures so I can guide you precisely of what is happening.我附上了一些屏幕截图,以便我可以准确地指导您了解正在发生的事情。
On this first one, I start to write on the search input.在第一个上,我开始写搜索输入。 I fill it up with whichever info I want to.我用我想要的任何信息填充它。
On this second one, when I do the search and I click the button, it spits out the error: Uncaught SyntaxError: Invalid or unexpected token
This is what happens in my development server.在第二个中,当我进行搜索并单击按钮时,它会吐出错误: Uncaught SyntaxError: Invalid or unexpected token
错误Uncaught SyntaxError: Invalid or unexpected token
这是我的开发服务器中发生的情况。
Thanks谢谢
I found the problem finally after a long time.找了好久终于找到问题了。 In the line where the function is being called on the onclick event inside this view, I had applied a json_encode function with PHP upon the name and therefore I was able to solve and do the query.在此视图内的 onclick 事件上调用该函数的行中,我在名称上应用了一个带有 PHP 的 json_encode 函数,因此我能够解决并执行查询。 That way the value's name I was searching for is being returned with json and there is no need of escaping the single quote character or doing other superfluous conversions.这样,我正在搜索的值的名称将与 json 一起返回,并且无需转义单引号字符或进行其他多余的转换。
<section class="item list-group-item">
<input type="hidden" id="treat_id" value="treatment_<?php echo $id;?>">
<input type="hidden" id="treat_name" value="<?php echo $name;?>">
<a onclick='set_id_article_popover("treatment_<?php echo $id;?>", <?php echo json_encode($final_name); ?>)' class="center-block thumb_xxs">
<i class="fa fa-heart"></i>
<?php
if(strlen($name) > 50){
$name_short= substr($name, 0, 50).' ...';
}else{
$name_short = $name;
}
$bname = returnBold($name_short, $search_text);
?>
<span title="<?php echo $name ?>" class="text-muted"><?php echo $bname; ?></span>
</a>
</section>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.