I'm trying to prevent my page from refreshing after i hit return in my textfield:

<input onkeyup="keyPressCodetitle(event, false, false, 0, 6, 20);" class="form_field" id="codeTitle" type="text" name="codename">

But somehow the keyPressCodetitle function will not prevent this even with the (as far as i know) neccesary requirements to stop it from refreshing the page. (It is however showing alerts etc. in the if statement accordingly. So the function and if are working properly)

Am i missing something perhaps?

function keyPressCodetitle (e, caseBoolean, isFragment, generalCode, sourceId, projectId, method) {
    //Get the key entered via onpress(Which for firefox)
    var keycode = (e.keyCode ? e.keyCode : e.which);
    if(keycode == '13'){
        e.preventDefault();
        //Functioncall here
    } else {
        //other function here when not pressing return  
    }
    return false;
}

Thanks in advance,

没有正确的解决方案

其他提示

It's not that "[Return] makes the page refresh"; It's that some (many?) browsers, by default, will treat a [Return] in text inputs as a sign of form submit-ion.

And that seems to happen on key down, so preventing this upon key up is, too late.

Add an extra listener to onkeydown may help:

<input onkeydown="preventSubmit(event);" onkeyup="keyPressCodeTitle(event);" />

JSFiddle demo

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top