views:

123

answers:

8

EDIT: For some reason if I change the input into an , the submit code works fine. Ok, this works, I'll just style the a tag to look like an input tag in css.

I am using a jQuery function to submit a form when a certain button is pressed, however this seems to have no effect on the form.

My code is as follows: HTML:

<form id="loginForm" action="" method="POST">
    <input class="loginInput" type="hidden" name="action" value="login">
    <input id="step1a" class="loginInput" type="text" name="username">
    <input id="step2a" class="loginInput" type="password" name="password"  style="display:none;">
    <input id="step1b" class="loginSubmit" onclick="loginProceed();" type="button" name="submit" value="Proceed" title="Proceed" />
    <input id="step2b" class="loginSubmit" onclick="submitlogin();" type="button" value="Validate" title="Validate"  style="display:none;" />
</form>

Javascript:

function submitlogin()
{
    $("#loginForm").submit();
}

function loginProceed()
{
    $("#step1a").fadeOut("slow",function(){
        $("#step2a").fadeIn("slow", function(){
            $("#step2a").focus();
        });
    });
    $("#step1b").fadeOut("slow",function(){
        $("#step2b").fadeIn("slow");
    });
    $("#step1c").fadeOut("slow",function(){
        $("#step2c").fadeIn("slow");
    });

}

However, when I press the button, absolutely nothing occurs.

PS. This function may seem meaningless since I can just use a input type="submit" but I originally intended this to have some more functionality, I stripped the function to its bare bones for testing purposes.

+4  A: 

You need to specify one form.

$("#loginForm").submit();
Pekka
Oh sorry, I forgot to say, I did that and it didn't work either.
Razor Storm
@Razor then there should be an error message in Firefox's error console. Are there any?
Pekka
No errors in the error console, no warnings either.
Razor Storm
@Razor strange. are you 100% sure that the form's `submit` is not overridden at some point, returning `false`?
Pekka
Hmm it doesn't appear to be. This is the link of the site: http://operation.mylifeisberkeley.com/
Razor Storm
A: 

Additionally, your action attribute in your form tag is empty. What do you expect to happen when the form is submitted?

Pablo
When `action` is empty, the form will target the current page.
Pekka
Because when action attribute is empty, the form will direct to the current page. I know nothing occurs because the page does not refresh.
Razor Storm
A: 

How can you be sure that nothing occurs when action attribute is empty?

tambourine
When `action` is empty, the form will target the current page.
Pekka
Because when action attribute is empty, the form will direct to the current page. I know nothing occurs because the page does not refresh.
Razor Storm
A: 

There's no jquery 'submit' method (not for ajax, at least): http://api.jquery.com/category/ajax/

You probably want to invoke form's submit method:

$("#loginForm")[0].submit();

Remember, jquery selector always returns array.

edit
'submit' will actually bind handler to submit event, not submit form:
http://api.jquery.com/submit/

Nikita Rybak
I tried that and firefox error console says "$("#loginForm")[0].submit() is not a function". It didn't say anything when I did $("#loginForm").submit();
Razor Storm
Does this work? document.forms["loginForm"].submit();
Nikita Rybak
No, it does not. :[
Razor Storm
I fiddled a bit with it: if you change 'name="submit"' to something else (like 'name="submit2"'), then '$("#loginForm")[0].submit();' will actually submit form.
Nikita Rybak
+3  A: 

EDIT: Additional information added to question. You appear to be calling the wrong function. The submit button that is not display:none calls loginProceed() not submitlogin().

Also, if the functions are defined within jQuery's ready() function, they will be out of scope unless you define them as global.

Live example: http://jsfiddle.net/eSeuH/

Updated example: http://jsfiddle.net/eSeuH/2/

If the code you noted in the comment runs before the DOM is loaded, it will not work. You need to ensure that it does not run until the DOM has loaded (or at least the element it references has loaded).

$(document).ready(function() {
    $("#loginForm").submit(function() { alert("clicked"); });
});
patrick dw
+1 quite a lot of bugs to haunt :)
Nikita Rybak
loginProceed() simply makes all the invisible form elements visible, and makes the previously visible ones invisible. So after that is called, the submit button that was once display:none will no longer be. That's the button that matters, and it calls submitlogin()
Razor Storm
@Razor - Ok, based on the information given that wasn't clear. How about the scope of the functions? Where are they defined? See live example I posted in my answer.
patrick dw
Ok they are in the global scope. I tried yours in the live example. I also tried this: $("#loginForm").submit(function() { alert("clicked"); });Yet the alert doesn't even pop up.
Razor Storm
Also, if I put an alert inside the body of the submitlogin() function, it does show up, so I know the correct function is being called.
Razor Storm
@Razor - The code you added in your comment `$("#loginForm").submit(function() { ...` won't work unless it is run after the DOM has fully loaded. Is that the case?
patrick dw
@Razor - I updated the example, wrapping your new code in `ready()` so that it runs after the DOM is loaded. Works perfectly.
patrick dw
A: 

Try look in to Firefox debug console. Maybe you have errors in javascripts??? Because even if action is empty, all works.

Saff
A: 

For some reason if I change the input into an , the submit code works fine. Ok, this works, I'll just style the a tag to look like an input tag in css.

Razor Storm
+4  A: 

Try to use another name for input with name="submit". Without this it works fine for me.

tambourine
Oh wow this fixed it!
Razor Storm