简体   繁体   中英

JavaScript Change default button prob with firefox

following problem:

I have a list with buttons, and after that a textbox with a button to add a new entry. In both, the list buttons, and the add button are JavaScript routines to confirm the action.

Now I have the problem, that, when the user presses enter in the textbox, the first button if the list will be invoked instead of the add button. So I have added JavaScript in the "onkeydown" event of the button, where I use "return false;" to avoid that the first button will be invoked after i have checked that the return button was pressed. This works perfectly with following line: onkeydown="if (event.which == 13) { return false; }" But as soon as I add any JavaScript code before the "return false;" (I need a confirm message there and some other JavaScript routines), the firefox browser does invoke the first button AND the javascript code in the onkeydown event... This behavior does not seem to occur on Internet Explorer.

I have tried for hours to find a solution, but I can't find one and I also can't find the right hint on the internet. So I would be very glad if someone could help me on this problem?

I have tried to build up a code sample, where everyone can reproduce the wrong behavior. In the last input control i have added the onkeydown event. Could someone please tell me why Firefox displays the confirm message of the first button, when I press the Enter button in the text input field?

<html>
<head>
</head>
<body>
    <form method="post" action="Test.html" id="ctl00">
    <div id="divTest">
        <table>
            <tr>
                <td>
                    Testline 1
                </td>
                <td>
                    <input type="submit" onclick="if (confirm('Sure?')) alert('the wrong postback'); else return false;" value="Test1" />
                </td>
            </tr>
            <tr>
                <td>
                    Testline 2
                </td>
                <td>
                    <input type="submit" onclick="if (confirm('Sure?')) alert('the wrong postback'); else return false;" value="Test2" />
                </td>
            </tr>
        </table>
    </div>
    <table>
        <tr>
            <td>
                Enter name:
            </td>
            <td>
                <input type="text" size="30" onkeydown="if (event.which == 13) { alert('test'); return false; }" />
            </td>
            <td>
                <input type="submit" title="Add it" onclick="alert('the correct postback')" />
            </td>
        </tr>
    </table>
    </form>
</body>

Replace everywhere

type="submit"

with

type="button"

so that hitting "enter" won't submit the form.

But the entire scheme is strange : do you want to submit the form or not ? If so, why not having (as is usual) only one function checking all fields ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM