获取未捕获错误:语法错误,无法识别的表达式 [英] Getting Uncaught Error: Syntax error, unrecognized expression

查看:106
本文介绍了获取未捕获错误:语法错误,无法识别的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从下面粘贴的HTML中获取 Contact_LastName 的值,但是我收到以下错误

I am trying get the value of Contact_LastName from the HTML pasted below but I am getting the following error

Uncaught Error: Syntax error, unrecognized expression: div #u1@g.com

Javascript:

Javascript :

 $('.rdContact').off("click").on("click", function (e) {
                              e.preventDefault();
                              $(this).next("div").toggle();  

                              $('.btn-success').off('click').on('click', function (e) { 
                                 e.preventDefault();
                                 var divId = $(this).closest('div').parent().attr('id');
                                 var lnameval = $("div #"+ divId).closest('#Contact_LastName').val();
                                 alert(lnameval);  
                              });

                          });

HTML:

<input type="radio" class="rdContact" name="ContactGroup" value="u1@g.com" style="">

<div class="row" id="u1g.com" style="display: none;">
        <div class="col-md-6">

            <div class="form-group">
                <div class="col-md-3 control-label">
                    <label>Last Name:</label>
                </div>
                <div class="col-md-3">
                    <input class="form-control pr_input defaultText quiet reqrd" id="Contact_LastName" maxlength="50" name="ContactLastName" type="text" value="">
                </div>
            </div>

        </div>
        <div class="col-md-6">
            <div class="form-group">
                <div class="col-md-3 control-label">
                    <label>First Name:</label>
                </div>
                <div class="col-md-3">
                    <input class="form-control defaultText quiet reqrd" id="Contact_FirstName" maxlength="50" name="ContactFirstName" type="text" value="">
                </div>
            </div>               
        </div>


        <div class="modal-footer">                 
            <input type="button" value="Add Contact" id="btnSaveContact" class="btn btn-success">
            <input type="button" value="Cancel" id="btnCancelContact" class="btn btn-default">    
        </div>
    </div>


推荐答案

这种情况正在发生,因为 u1 @ g.com 不是元素ID的有效值,因此jQuery无法解析您的选择器。

This is happening because u1@g.com is not a valid value for an element's ID, so jQuery is unable to parse your selector.

来自 HTML4规范



ID和NAME令牌必须以字母([A-Za-z])开头,后面可以跟任意数量的字母,数字([0- 9]),连字符( - ),下划线(_),冒号(:)和句号(。)。

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").


解决方案是为您的ID使用有效值。

The solution is to use a valid value for your ID.

参见

Re:你的评论如下,你得到 undefined 因为您使用的是 nearest() 错误(请点击该链接查找出什么最近()实际上是这样做的。

Re: your comment below, you are getting undefined because you are using closest() wrong (please follow that link to find out what closest() actually does).

我愿意打赌你有 id Contact_LastName 在您网页的多个位置,这是一个大问题,因为ID必须是唯一的。

I'm willing to bet that you have the id Contact_LastName in multiple places in your page, and that is a big problem because ids have to be unique.

我也很好奇为什么你选择 div并检索它的ID,这样你就可以了使用该ID在下一行重新选择它。

I am also very curious why you are selecting the row div and retrieving its ID, just so that you can use that ID to re-select it in the next line.

取代您当前的方法,删除重复的ID并使用它:

Instead of your current approach, get rid of your duplicate IDs and use this:

$('.btn-success').off('click').on('click', function (e) { 
    e.preventDefault();
    var row = $(this).closest(".row");
    var lnameval = row.find("input[name='ContactLastName']").val();
    alert(lnameval);  
});

这篇关于获取未捕获错误:语法错误,无法识别的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆