Here's a form that I have:
<form class="form-horizontal" id="login-form">
<input name="email" id="email" type="email" size="30" class="span4" placeholder="Email" style="margin-top:20px; margin-left:20px; width: 270px;"/>
<br>
<input name="passwd" id="passwd" type="password" class="span4" placeholder="Password" style="margin-top:10px; margin-left:20px; width: 270px;"/>
<br><br>
<input type="button" id="login" name="login" value="Go" class="btn btn-custom" style="width:30%; margin-left:30px; margin-top:20px"/>
<br>
<br>
</form>
Here a ajax call I make using JavaScript to submit the data on clicking the button:
$('#login').click(function(){
$.ajax({
url:"script.php",
type:'POST',
dataType:"json",
data: $("#login-form").serialize()
}).done(function(data){
//do domething
});
});
How can I submit the data both by pressing the enter key and clicking on the submit button?
I have updated code. Create a function and call it on both textbox keypress event and identify submit button then call function and on click event of button.
function submitLogin() {
$.ajax({
url:"script.php",
type:'POST',
dataType:"json",
data: $("#login-form").serialize()
}).done(function(data){
//do domething
});
}
$('#email').keypress(function(e) {
if (e.which == '13') {
submitLogin();
}
});
$('#passwd').keypress(function(e) {
if (e.which == '13') {
submitLogin();
}
});
$('#login').click(function(){
submitLogin();
});
How about to change input type to "submit" and input click event into form submit?
<input type="submit" id="login" name="login" value="Go" class="btn btn-custom" style="width:30%; margin-left:30px; margin-top:20px"/>
JQuery
$('#login-form').submit(function(){
$.ajax({
url:"script.php",
type:'POST',
dataType:"json",
data: $("#login-form").serialize()
}).done(function(data){
//do domething
});
});
You should be able to add keypress function on both email and password fields.
$('#email').keypress(function(e){
if (e.keyCode == 13) {
$('#login-form').submit(/*...*/);
}
}
I gave you example for one field.
this is the idea:
$("input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
runAjax();
// or
// runSubmit();
}
});
$('#login').click(function(){
runAjax();
//or
//runSubmit();
});
function runAjax(){
$.ajax({
url:"script.php",
type:'POST',
dataType:"json",
data: $("#login-form").serialize()
}).done(function(data){
//do domething
});
}
function runSubmit(){
$("form").submit();
}
Use On method to work on Entering and clicking
$('#element').on('click keypress', function(event) {
var check = 1; // Default for Click
if (e.keyCode == 13) //When Enter Key is pressed
{
var check = 1;
}
else
{
var check = 0; // other keys pressed
}
if (check == 1)
{
$.ajax({
url:"script.php",
type:'POST',
dataType:"json",
data: $("#login-form").serialize()
}).done(function(data){
//do domething
});
}
});
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.