2

This is the code of an HTML form as shown by Firebug:

<form action="http://localhost/home/newuser" method="post">
    <input type="hidden" value="48" name="user_id">
    <input type="text" name="username">
    <input type="submit" value="Next">
</form>

And here's some jQuery code bound to it:

$('.popup .newuser form').live('submit', function()
{
    $(this).ajaxSubmit(function(data)
    {
     // handle response
    });
    return false;
});

However, when I click on the "Next" button, what happens is that the text field shows a combobox of previously entered values (a Firefox feature). There is no HTML request showing up on Firebug's Network tab, and breakpoints in the jQuery listener are not hit.

It feels like I'm missing something really obvious...

Update: I've realized that something even stranger is going on: I cannot enter anything in the text field either via keyboard. I can only select the previous values from the Firefox combobox. And after doing that, I can't even select the text in the box.

9
  • 1
    Can you give the response Code, which you have commented out (// handle response) ? Commented Apr 10, 2012 at 11:55
  • I don´t sure about your bug. I think you need any ext on you action. you use html, php, jsp or .do? action="localhost/home/newuser.php" I hope it help you Commented Apr 10, 2012 at 12:01
  • Sounds like either the live isn't successfully wiring up to the event, or maybe the selector has an issue? I know it sounds obvious, but are the popup and newuser classed tags surrounding the form? Is it possible that popup and newuser are sibling classes (i.e. <div class="popup newuser">)? If that's the case, then the selector should be $('.popup.newuser ...') without the space. Commented Apr 10, 2012 at 12:04
  • @DRP96: since it's not being executed, it shouldn't matter. Commented Apr 10, 2012 at 12:07
  • @ataddeini: I just put a breakpoint into the jQuery live() function, and it was hit, and on the correct DOM element. Commented Apr 10, 2012 at 12:24

5 Answers 5

1

It turns out the problem had nothing to do with jQuery or Javascript; instead it lay in the CSS, where a DIV containing the form had a z-index of 1, and was itself contained in a jQuery-UI dialog, which the framework gave a z-index of 1001. Apparently this caused the inner DIV (and thus the form) to not receive any keyboard or mouse events at all.

Sign up to request clarification or add additional context in comments.

Comments

0
<form action="http://localhost/home/newuser" method="post">
    <input type="hidden" value="48" name="user_id" />
    <input type="text" name="username" />
    <input type="submit" value="Next" />
</form>

Put / at the end of the input element

1 Comment

It's actually there in the HTML, what I have in the question is the normalized HTML shown by firebug
0

Why don't you use <form ... OnSubmit="yourfunction()"></form> ?

1 Comment

Because the whole app is based on unobtrusively attached jQuery event handlers.
0

I would have put this in a comment but it seems I don't yet have the privilege of doing so.

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

EDIT

Separate your selectors with commas rather than spaces:

$('form, .popup, .newuser')

Hope that helps

3 Comments

Interesting - but looking at the jQuery code, I see that live() just forwards to on() anyway. And no, the callback is not triggered - it seems like no submit is actually happening.
I'm pretty sure it must have something to do with the selectors. I just set up pretty much identically and tested, didn't see anything in Firebug console, I changed the selector to just $('form') and I can see the POST requests straight away.
The selector works. The problems seems to be afterwards, events getting lost.
0

.live() is deprecated now. For cross-browser support, use .on() when you are using jQuery 1.7+ or .delegate() in lower versions.

.on() example :

$(document).on('submit', '.popup .newuser form', function(){
    $(this).ajaxSubmit(function(data){
       // handle response
    });
    return false;
});

.delegate() example :

$(document).on('.popup .newuser form', 'submit', function(){
    $(this).ajaxSubmit(function(data){
       // handle response
    });
    return false;
});

4 Comments

Using on() makes no difference, and the callback is never executed.
Is the .on() not invoking or .ajaxSubmit() not invoking ?
is the action url http://localhost/home/newuser is of same domain or different ? This could happen if you are doing cross-domain AJAX request.
have a look at developer.mozilla.org/en/Same_origin_policy_for_JavaScript then you will be sure whether it is a cross-domain request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.