views:

43

answers:

3

This is what I want to happen.. When a user hits 'enter' a 'SearchForItems()' should trigger. What is happening is another control, with 'other button', on the page gets focus returned to it anytime any other element is tabbed out of or after onkeyup.

I am having to force focus on the 'Search button' on page load, but after that any time enter is pressed the 'other button' is triggered. The 'other button' does not have a tab index assigned and is the pages default button.

I'm not sure if I've done a good job explaining this. Basically I want to know why focus is being shifted and how I can force the 'Search button' to trigger anytime enter is pressed.

this is one entry on my page where I'm trying to force the 'Search button' to trigger:

<td><input type="text" onkeydown="CheckForEnterKey(event)" id="AcceptedDate1" maxlength="7"  style="width:121px;" value="<%=DateTime.Now.AddMonths(-3).ToString("MM/yyyy")%>"/>&nbsp;</td>

this is part of my jquery file where i'm also

$('#AcceptedDate1').keypress(function(e) { if (e.keyCode == 13 || e.which == 13) $('#SearchAcceptedLotsButton').click(); });

CheckForEnterKey Function

function CheckForEnterKey(e)
    {
        var keycode;
        if(e.which)
        {
            keycode = e.which;
        }
        else
        {
            keycode = e.keyCode;
        }

        if(keycode == 13)
        {
            $('#SearchAcceptedLotsButton').click();//tried SearchForItems() also
        }
    }

Edit:

I just added this to my page and it appears to work, I added this to the main div thats holding everything together. What I need to do now is figure out where the enter was pressed so the enter is canceled everytime it is pressed within the main portion of my page. Any ideas, there are a lot of elements on my page?

function checkKey(e)
    {
        if(e.keyCode == 13 || e.which == 13)
        {
            e.returnValue = false;
            e.cancel = true;
        }
    }
A: 

I am not sure why you are having problems. But I can offer you my onKeyDown code for pressing enter:

if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) { #DO JS HERE # }}

So I use it like this:

<input type="text" onKeyDown="if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) { alert('You pressed enter!'); }}">
Badger
A: 

In the above code whenever enter is pressed the id="SearchAcceptedLotsButton" can be triggered click as follows :

    if(keycode == 13)
    {
        $('#SearchAcceptedLotsButton').trigger('click');
    }
Alpesh
i've tried this, thanks. Since the other button is the default button for the page, I need to override that behavior.
Avien
A: 

this is how I resolved the issue:

I added this function..

function checkKey(e)
    {
        if(e.keyCode == 13 || e.which == 13)
        {
            e.returnValue = false;
            e.cancel = true;
            SearchAcceptedLots();
        }
    }

At the top most div containing everything on the page I added this:

<div id="ItemPanel" onkeydown="checkKey(event)">

this catches every enter that is pressed within the form, canceling the default buttons behavior and calling the desired function.

Avien