I want to link an Enter Button (on a keyboard) to text box in javascript and the problem is there are two submit buttons linked to it. I researched a bit and realized that i can use keypress like this
$('#searchbox input').bind('k开发者_C百科eypress', function(e) {});
If there is one submit button.
Does any one have idea on how to make it work for my code?
html:
<form id="myForm" method="post">
id: <input type="text" name="id" id="id"/>
<div id="hidden" style="display: none;">
age: <input type="text" name="age"/>
<input type="button" id="button2" value="Update" />
</div>
<input type="button" id="button1" value ="Get Info" onclick="document.getElementById('hidden').style.display = ''; this.style.display = 'none'" size="25"/>
Jquery code:
$(document).ready(function(){
$("#button1").click(function(){
$.post('fet.php', { id: $('input[name="id"]', '#myForm').val() },
function(json) {
$("input[name='age']").val(json.age);
}, "json");
});
$("#button2").click(function(){
$('form#myForm').attr({action: "fetch2.php"});
$('form#myForm').submit();
});
});
Thanks in advance!
You could do:
$('#searchbox input:text').keypress(function(e) {
e.preventDefault();//avoid submitting the form
if(e.which == 13){
//Enter pressed
}
});
Try this
$('#searchbox input:text').bind('keypress', function(e) {
if(e.which == 13){//Enter pressed
//Do your stuff here
}
});
精彩评论