Question

I want to prevent form submitting by hittiing enter key on input type = file. I searched the jquery script to do it. It is ok for input type = text but, if hit on input type file, the login page is displayed after log out. I'm using jsp and jquery. And also my brower is IE9. Is there something wrong in my code?

jquery :

$(function(){
    $(window).keydown(function(event){
        if(event.keyCode == 13) {
            event.preventDefault();
            //$('#fil_CsvImp_refer').click();
            return false;
        }
    });
    $("input[type=text]").keypress(function(ev) {
        if ((ev.which && ev.which === 13) ||
            (ev.keyCode && ev.keyCode === 13)) {
            return false;
        } else {
            return true;
        }
    });
    $("input[type=file]").keypress(function(ev) {
        if ((ev.which && ev.which === 13) ||
            (ev.keyCode && ev.keyCode === 13)) {
            return false;
        } else {
            return true;
        }
    });
});

jsp :

<s:form action="" method="POST" name="frm_CsvImp_import" id="frm_CsvImp_import"
enctype="multipart/form-data">
<s:file key="fil_CsvImp_refer" name="fil_CsvImp_refer" cssClass="mediuminput" accept=".csv" />
<input type="button" class="" value="submit"/>
</s:form>
Était-ce utile?

La solution

It works for me (in a file input, plain html on firefox, chrome (both upToDAte) and jquery 1.11):

     $("input[type=file]").keypress(function(ev) {
               return false;
               //ev.preventDefault(); //works as well

     });

Also notice that, not just "enter" key trigger the browser dialog (barspace does it as well).

Autres conseils

I think i didn't understood your question clearly

$(document).ready() {
$('input:file').keypress(function() {
  //your operations
});
}

(OR) once try this,hope it helps you

$(function(){

$("#frm_CsvImp_import").on("change", "#INPUTTYPEID( or.CLASSNAME)", function(){

//do your operations
});

});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top