tags:

views:

28

answers:

2
<form action="#" id = "form1">    
     <input type="submit" value="submit" />
</form>
<input type="button" onclick="$('#form1').submit();" value="submit2" />
<script type="text/javascript">
    Sys.UI.DomEvent.addHandler(
        document.getElementById("form1"),
        'submit',
         function() { alert("abc"); return false; }
         );
</script>

When I click submit2, it doesn't alert, why?

And how can I fix this?

A: 

This does not trigger a submit of the form:

<input type="button" onclick="$('#form1').submit();" value="submit2" /> 

From the jQuery documentation: 'Note: This does not execute the submit method of the form element! If you need to submit the form via code, you have to use the DOM method, eg. $("form")[0].submit();'

mnemosyn
Care to link where you found that? It isn't here: http://api.jquery.com/submit/
Nick Craver
The jQuery "submit()" is equivalent to "trigger('submit')", so it's different from calling the "native" form submit. Specifically, it'll do any sort of jQuery handlers, and then (if the handlers haven't killed the event) will call the native function. The "submit" function wired into the browser will just submit the form and it won't call any event handlers. (I know you know that, @Nick, but there might be young children reading this.)
Pointy
Frankly, I don't know. I have a pdf floating around where I found that line and I just trusted it - it doesn't seem to be an official documentation, though. According to the link you posted, there seems to have been a change in jQuery 1.4: 'as of jQuery 1.4, which has normalized the event's behavior.' (whatever that means, in detail).
mnemosyn
A: 
$("form").bind("submit", function(e){
    var formContext = this[Sys.Mvc.FormContext._formValidationTag];
    if(formContext != null) formContext._form_OnSubmit(e);
});
ldp615