Submit form on Enter key with javascript

2019-01-14 12:07发布

I'm not sure what I am doing wrong here. I want the enter key to work as well as clicking the button.

<form action="" method="get" class="priceOptionForm" name="priceOptionForm">
<input name="paypal_email" type="text" value="whatever" id="email"></label>
<a href="javascript:void(0);" class="bluebtn" id="profile_price" style="width:60px;margin-top:5px;">Save all</a>
</form>

7条回答
The star\"
2楼-- · 2019-01-14 12:59

All below codes should be added into script block or file. define submit function:

function submitForm(){
    document.priceOptionForm.submit();
    document.priceOptionForm.method='post';
}

For the enter key to submit form:

document.onkeydown=function(){
    if(window.event.keyCode=='13'){
        submitForm();
    }
}

For the link to work:

document.getElementById("profile_price").onclick=submitForm;

You can refer to http://jsfiddle.net/honglonglong/YMX2q/ for some trying.

查看更多
登录 后发表回答