jquery Uncaught SyntaxError: Invalid or unexpected token - Input text has an apostrophe
jquery Uncaught SyntaxError: Invalid or unexpected token - Input text has an apostrophe
我目前遇到的问题是关于用于搜索大量文章的输入。
为了更深入,问题是当我插入带有撇号的文本时,出现以下错误: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);
是我插入文章名称的地方。
$('#articles_search_input').val(id);
获取要搜索的文章的id。
函数中正确插入第一个数字,即文章的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>
现在,我将把 Controller 的部分附在您身上,以便您更好地了解 back-end 中发生的事情。
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'];
}
}
}
你知道是否有可能通过 Cake 转换撇号,或者通过 Javascript 转换撇号会更好吗?
我尝试通过将撇号替换为转义符和替代编码字符来应用一些解决方案,以便更简洁地阅读输入,但我有点 dead-end 在这里。任何帮助将不胜感激。
我附上了几张屏幕截图,以便我可以准确地指导您了解正在发生的事情。
在第一个上,我开始写搜索输入。
我用我想要的任何信息填充它。
在第二个上,当我进行搜索并单击按钮时,它吐出错误:Uncaught SyntaxError: Invalid or unexpected token
这就是我的开发服务器中发生的事情。
谢谢
好久好久终于找到问题了。
在此视图内的 onclick 事件上调用该函数的行中,我在名称上应用了一个带有 PHP 的 json_encode 函数,因此我能够解决并执行查询。
这样,我正在搜索的值的名称将与 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>
我目前遇到的问题是关于用于搜索大量文章的输入。
为了更深入,问题是当我插入带有撇号的文本时,出现以下错误: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);
是我插入文章名称的地方。
$('#articles_search_input').val(id);
获取要搜索的文章的id。
函数中正确插入第一个数字,即文章的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>
现在,我将把 Controller 的部分附在您身上,以便您更好地了解 back-end 中发生的事情。
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'];
}
}
}
你知道是否有可能通过 Cake 转换撇号,或者通过 Javascript 转换撇号会更好吗?
我尝试通过将撇号替换为转义符和替代编码字符来应用一些解决方案,以便更简洁地阅读输入,但我有点 dead-end 在这里。任何帮助将不胜感激。 我附上了几张屏幕截图,以便我可以准确地指导您了解正在发生的事情。
Uncaught SyntaxError: Invalid or unexpected token
这就是我的开发服务器中发生的事情。
谢谢
好久好久终于找到问题了。 在此视图内的 onclick 事件上调用该函数的行中,我在名称上应用了一个带有 PHP 的 json_encode 函数,因此我能够解决并执行查询。 这样,我正在搜索的值的名称将与 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>