English 中文(简体)
uncaught exception: Syntax error, unrecognized expression: #
原标题:

I get this error on a .click() event in jQuery. I see it in Firebug. I use the latest version, 1.3.2 (min) The click fires an $.ajax() request for a Form in my website. I asked google about this and all he knows is "%" or "[@]" as unrecognized expressions, nothing about the "#".

here is a bit of my code:

    $("form#buyForm #submitForm").live("click", function(e) {
        var errors = 0;

        var inputLastName_value = $("form#buyForm input#userLastName").val();
        if (inputLastName_value == "") {
         errors = 1;
         formErrorHandling("#userLastName");
         return false;
        }
        return false;
    });

This way I check all my inputs for errors, and then call formErrorHandling() who does some show/hide, stuff like that, nothing important.

I read that it might be from a selector of mine, but they all seem to be just fine.

Anybody else had the same problem?

Thanks.

最佳回答

From what I can see the exception seems to be somehow triggered by the jScrollPane plugin you are using.

Try replacing the script you include (v1.2.3 which is as old as from Dec 2008) with a newer version directly from the trunk. Which includes many improvements over v.1.2.3 and fixes the exception for me jScrollPane.js (jScrollPane.min.js minified version of r87 jScrollPane minified with YUICompressor)


removed old answer stuff no longer needed

问题回答

In some browsers id might be empty and JQuery chokes on "#" as a selector.

Sept 2011

changed from

$(document).ready( function () {
  $( # +id).creatorCall( {init:param} ) ;
} );

to

$(document).ready( function () {
  $(this).creatorCall( {init:param} ) ;
} );

and cured that self same uncaught exception: Syntax error, unrecognized expression: # message

what does the formErrorHandling function look like? Is it expecting a selector or a jQuery object?

perhaps what you need is to call it like

formErrorHandling($("#userLastName"));

I know it s an old issue and there s newer version jScrollPane but as I needed to use current one for due to a legacy issues here s the fix. If you ll use jScrollPane.js from the answer above you need to change the code in line 534:

change:

    if (h && h.substr(0, 1) ==  #  && h.length > 1) {

to

    if (h && h!= #  && h.substr(0, 1) ==  #  && h.length > 1) {

Whole chunk of the code responsible for document clicks with the fix:

$(document).bind( click , function(e){
    $target = $(e.target);
    if ($target.is( a )) {
        var h = $target.attr( href );
        if (h && h!= #  && h.substr(0, 1) ==  #  && h.length > 1) {
            setTimeout(function(){
                scrollTo(h, !settings.animateToInternalLinks);
            }, $.browser.safari ? 100 : 0);
        }
    }
});

Basically the scrollTo function will be ignored if link s href equals #

Cheers

G.

The problem for me seemed to be caused by having one too many # in the selector.

For example:

$( ##id_name )

should have been...

$( #id_name )




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签