This is a jquery question rather than a web2py question but I would be
really grateful if someone could help with a problem that is driving
me crazy.

I have two forms. I want to submit a form automatically when it loses
focus. I am doing this by capturing the focusin event and calling
triggerHandler('submit'). It works when I mouse click the focus
between the two forms. The problem is when I capture a keypress and
call focus(). In this case it calls focusin and
triggerHandler('submit'). However triggerHandler does not call the
listener but returns undefined, implying there are no listeners for
the event.

Can anyone provide any insight into this?

My test code is below if that helps explain:


<script src={{=URL('static', 'js/jquery.js')}} type="text/
javascript"></script>

<form id="form1" name="form1" action="form1action" method="get">
        Field1:
        <input type="text" id="field1" name="field1" />
        <input type="submit" value="Submit" />
</form>
<br>
<br>
<form id="form2" name="form2" action="form2action" method="get">
        Field2:
        <input type="text" id="field2" name="field2" />
        <input type="submit" value="Submit" />
</form>

Last key press:
<span id='keypressed'></span>
<br>
Last focus form:
<span id='focusform'></span>
<br>
Pre-submit:
<span id='presubmit'></span>
<br>
Result:
<span id='result'></span>
<br>
Last submit:
<span id='submitted'></span>
<br>

<script>
        $(document).ready( function() {

                $(document).keydown( function(e) {
                        if (e.which==39) {
                                $('#keypressed').html("right key pressed")
                                var focusform=$('#focusform').html()
                                if (focusform=="form1")
                                        $('#field2').focus()
                                else if (focusform=="form2")
                                        $('#field1').focus()
                        }
                        else
                                $('#keypressed').html("another key pressed")

                })
                $('#form1').submit( function() {
                        $('#submitted').html("form1")
                        return "subed"
                })
                $('#form2').submit( function() {
                        $('#submitted').html("form2")
                        return "subed"
                })
                $('form').focusin( function() {
                        var focusform=$('#focusform').html()
                        if (focusform=="form1") {
                                $('#submitted').html("")
                                $('#presubmit').html(focusform)
                                $('#result').html("")
                                result=$('#form1').triggerHandler('submit')
                                $('#result').html(result)
                        }
                        else if (focusform=="form2") {
                                $('#submitted').html("")
                                $('#presubmit').html(focusform)
                                $('#result').html("")
                                result=$('#form2').triggerHandler('submit')
                                $('#result').html(result)
                        }
                        
$('#focusform').html($(this).closest('form').attr('name'))
                })
        })
</script>

Reply via email to