ok, so i've been searching for a piece of code that does this, and nothing applies to my situation.
My Problem: i'm trying to prevent the form from submitting when the "ENTER" key is pressed but allowing the my search script thats inside the form to submit when the enter key is pressed.
What i've found are scripts that prevent the Enter key period, or the script doesn't work because i think i'm using a <button>
instead of a <input>
as my submit button, i'm not really sure.
my form
<form action="" method="post" onsubmit="return false;" id="myform">
my javascript that does my search
function showSub(str) {
var xmlhttp;
if (str=="") {
document.getElementById("txtSub").innerHTML="";
return;
}
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtSub").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","findLogo.php?q="+encodeURIComponent(str),true);
xmlhttp.send();
}
$(document).on('change', '#searchText', function() {
// Get the search text
var searchText = $(this).val();
// Make sure it isn't empty
if (searchText.length > 0) {
// Send the update request
showSub(searchText);
}
});
my search input
<input type="text" id="searchText" value="" />
<div id="txtSub"></div>
my submit button
<input type="hidden" value="Post" name="submit" />
<button type="submit" style="height:33px; width:50px">
<img src="../css/images/plus_25.png" />
</button>
my java-script post submit script
<script type="text/javascript">
$(function(){
$('button[type=submit]').click(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "postadd.php",
data: $("#myform").serialize(),
beforeSend: function(){
$('#result').html('<div class="success"><img src="../images/loading-blue.gif" width="25" /></div>');
},
success: function(data){
$('#result').html(data);
$('html, body').animate({scrollTop:$(document).height()}, 'slow');
//document.getElementById('footer').scrollIntoView();
}
});
});
});