表格提交在jquery中输入按键

时间:2016-09-08 09:36:45

标签: javascript jquery

我需要在Enter Key上提交表格。我试过下面的代码,但没有发生任何事情。

我的代码在

下面
<script>
     $(function() {
$("#verifyForm").keypress(function (e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        $('button[type=submit]').click();
        validateSearch();
        return false;
    } else {
        return true;
    }
});

});        

    <form:form modelAttribute="verifyFormModel" method="POST" name="verifyForm" action="verifyForm" id="verifyForm" onsubmit="return(validateSearch());" >
                <fieldset>
                    <ul>
                        <c:if test="${aVerifyFormModel.errorFlag == 1}">
                            <div id="messageText">The code you entered is invalid. Please check and re-enter.</div>
                        </c:if>
                        <div id="formError" style="display: none;"></div>
                        <li><label for="headerTxt">Code: <span class="req">*</span></label> 
                            <input id="formCode" size="50" maxlength="6" type="text" name="formCode" />
                        </li>
                    </ul>
                    <div >
                        <Button id="Display" name="Display" >Submit</Button>
                    </div>
                </fieldset>
            </form:form> 

我的代码有什么问题?任何帮助表示赞赏

4 个答案:

答案 0 :(得分:1)

如果使用jquery:

,请尝试此操作
if(key == 13 || key == 3){
  $('#verifyForm').submit()
}

答案 1 :(得分:0)

通过ID获取表单:

 describe('testing redirection()', () => {
       beforeEach( inject(($state) => {
            //here I'm saying that I'm spying every call to $state.go
            spyOn($state, 'go');

            //And here I'm that I'm not only spying every call to 
            //controller.look() but I'm also replacing the original
            //implementation with my fake one. Every call will basically 
            //return an object with id equals 10
            spyOn(controller, 'look').and.callFake(() => {
                 var mockedLine = {
                     _id: 10
                 };
                 return mockedLine;
            });
       }));

       it('should call state.go', () => {
            controller.redireccion();

            //if you just want to check if the method was called, do the following:
            expect($state.go).toHaveBeenCalled();

            //if you need to also check the arguments, try:
            var args = $state.go.mostRecentCall.args;
            expect(args[0]).toBe('modifyLine');
            expect(args[1].lineId).toBe(10);
       });
  });

或完全jquery

document.getElementById("verifyForm").submit();

答案 2 :(得分:0)

你能试试吗??

$('#formCode').keypress(function (e) {
  if (e.which == 13) {
    $('#verifyForm').submit();
    return false;    
  }
});

答案 3 :(得分:0)

谢谢你们所有的时间,这段代码对我有用。

在脚本

中添加此代码
$(function() {
        $('#verifyForm').keypress(function(e) { //use form id
            if (e.which == 13) {
                validateSearch(); //-- to validate form 
                $('#verifyForm').submit();  // use form id
                return false;
            }
        });
    });