简体   繁体   English

jquery Uncaught SyntaxError:无效或意外的标记 - 输入文本有一个撇号

[英]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>&nbsp;<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>

未捕获的 SyntaxError:无效或意外的令牌<input id="{{" vendor.id }}< div><div id="text_translate"><p> 我正在尝试在 input[type='checkbox'] 上单击 function 提交表单,而不使用 ajax-jquery 刷新页面。 顺便说一句,我正在开发一个 Django web 应用程序。</p><p> 这是我的表单元素代码-</p><pre> {% if vendor.accountEnabled %} &lt;form id="form" action="{% url 'change_account_switch' vendor.id %}" method="post"&gt; {% csrf_token %} &lt;div class="custom-control custom-switch"&gt; &lt;input onclick="switchAcc('{{ vendor.id }}customSwitch1');" type="checkbox" name="switch" class="custom-control-input" id="{{ vendor.id }}customSwitch1" checked&gt; &lt;label class="custom-control-label" for="{{ vendor.id }}customSwitch1"&gt;Enable&lt;/label&gt; &lt;/div&gt; &lt;/form&gt; {% else %} &lt;form id="form" action="{% url 'change_account_switch' vendor.id %}" method="post"&gt; {% csrf_token %} &lt;div class="custom-control custom-switch"&gt; &lt;input onclick="switchAcc('{{ vendor.id }}customSwitch1');" type="checkbox" name="switch" class="custom-control-input" id="{{ vendor.id }}customSwitch1"&gt; &lt;label class="custom-control-label" for="{{ vendor.id }}customSwitch1"&gt;Enable&lt;/label&gt; &lt;/div&gt; &lt;/form&gt; {% endif %}</pre><p> 这是我的JS代码-</p><pre> function switchAcc(cb_id) { let switchBtn = $('#' + cb_id).is(':checked'); console.log(switchBtn) console.log(cb_id) $.ajax({ url: $('#form').attr('action'), data: { 'status': switchBtn }, dataType: 'json', success: function (data) { console.log(data) if (data.is_done) { alert("A user with this username already exists."); } }, error: function (e) { console.log(e.message); } }); }</pre><p> Django views.py ----</p><pre> @login_required(login_url='/login/') def change_account_switch(request, doc_id): ref = db.collection('vendorUsers').document(doc_id) switch = request.GET.get('status', None) if switch == 'true': switch = True elif switch == 'false': switch = False ref.update({ 'accountEnabled': switch, }) data = { 'is_done': ref.get().to_dict()['accountEnabled'], } return JsonResponse(data)</pre><p> 我还有一个问题。 看看我的表单元素代码,我正在显示多个用户,每个用户在他们的个人资料数据库中都包含 accountEnabled 字段。 . . .</p><p> 现在,看看我的views.py 代码。 在该文件中,我根据切换按钮(复选框)更新 accountEnabled 字段。 . . .</p><p> 但问题是,只有第一个用户的 accountEnabled 字段在列表中更新。 –</p></div> - Uncaught SyntaxError: Invalid or unexpected token in <input id={{ vendor.id }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery Uncaught SyntaxError:无效或意外的令牌 - jquery Uncaught SyntaxError: Invalid or unexpected token jQuery:未捕获的语法错误:无效或意外的令牌 - JQuery: Uncaught SyntaxError: Invalid or unexpected token 未捕获的SyntaxError:无效或意外的令牌 - Uncaught SyntaxError: Invalid or unexpected token 未捕获的 SyntaxError:无效或意外的令牌<input id="{{" vendor.id }}< div><div id="text_translate"><p> 我正在尝试在 input[type='checkbox'] 上单击 function 提交表单,而不使用 ajax-jquery 刷新页面。 顺便说一句,我正在开发一个 Django web 应用程序。</p><p> 这是我的表单元素代码-</p><pre> {% if vendor.accountEnabled %} &lt;form id="form" action="{% url 'change_account_switch' vendor.id %}" method="post"&gt; {% csrf_token %} &lt;div class="custom-control custom-switch"&gt; &lt;input onclick="switchAcc('{{ vendor.id }}customSwitch1');" type="checkbox" name="switch" class="custom-control-input" id="{{ vendor.id }}customSwitch1" checked&gt; &lt;label class="custom-control-label" for="{{ vendor.id }}customSwitch1"&gt;Enable&lt;/label&gt; &lt;/div&gt; &lt;/form&gt; {% else %} &lt;form id="form" action="{% url 'change_account_switch' vendor.id %}" method="post"&gt; {% csrf_token %} &lt;div class="custom-control custom-switch"&gt; &lt;input onclick="switchAcc('{{ vendor.id }}customSwitch1');" type="checkbox" name="switch" class="custom-control-input" id="{{ vendor.id }}customSwitch1"&gt; &lt;label class="custom-control-label" for="{{ vendor.id }}customSwitch1"&gt;Enable&lt;/label&gt; &lt;/div&gt; &lt;/form&gt; {% endif %}</pre><p> 这是我的JS代码-</p><pre> function switchAcc(cb_id) { let switchBtn = $('#' + cb_id).is(':checked'); console.log(switchBtn) console.log(cb_id) $.ajax({ url: $('#form').attr('action'), data: { 'status': switchBtn }, dataType: 'json', success: function (data) { console.log(data) if (data.is_done) { alert("A user with this username already exists."); } }, error: function (e) { console.log(e.message); } }); }</pre><p> Django views.py ----</p><pre> @login_required(login_url='/login/') def change_account_switch(request, doc_id): ref = db.collection('vendorUsers').document(doc_id) switch = request.GET.get('status', None) if switch == 'true': switch = True elif switch == 'false': switch = False ref.update({ 'accountEnabled': switch, }) data = { 'is_done': ref.get().to_dict()['accountEnabled'], } return JsonResponse(data)</pre><p> 我还有一个问题。 看看我的表单元素代码,我正在显示多个用户,每个用户在他们的个人资料数据库中都包含 accountEnabled 字段。 . . .</p><p> 现在,看看我的views.py 代码。 在该文件中,我根据切换按钮(复选框)更新 accountEnabled 字段。 . . .</p><p> 但问题是,只有第一个用户的 accountEnabled 字段在列表中更新。 –</p></div> - Uncaught SyntaxError: Invalid or unexpected token in <input id={{ vendor.id }} 未捕获的语法错误:如果在 jquery 中出现意外标记 - Uncaught SyntaxError: Unexpected token if in jquery jquery Uncaught SyntaxError:意外的令牌: - jquery Uncaught SyntaxError: Unexpected token : [jQuery]未捕获的SyntaxError:意外令牌{ - [jQuery]Uncaught SyntaxError: Unexpected token { jquery Uncaught SyntaxError:意外的令牌 - - jquery Uncaught SyntaxError: Unexpected token - Uncaught SyntaxError: Unexpected token &lt; in jQuery - Uncaught SyntaxError: Unexpected token < in jQuery jQuery-未捕获的SyntaxError:意外令牌) - jQuery - Uncaught SyntaxError: Unexpected token )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM