I have two search options: 1. On Master Page there is a text box and button for search. 2. on content page there is form for with two texboxes and a button for search.
Now whenever i press enter key from keyboard, the masterpage button event is fires. I mean in every case when I press enter key from keyboard the same event is called. I want If someone fill the content page search form and press enter key, it fires content page event.I am doing it like this:
<script language="javascript">
function HandleEnterKey(e) {
var key;
if (window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13) {
if (txtSearchHasFocus) {
alert(txtSearchHasFocus);
document.getElementById("ctl00_bluheader1_btnSearch1").click();
}
else if (txtBusinessNameHasFocus || txtLicNumberHasFocus) {
alert(txtBusinessNameHasFocus);
alert(txtLicNumberHasFocus);
alert(document.getElementById("ctl00_ContentPlaceHolder1_btnSearch"));
e.cancel = true;
document.getElementById("ctl00_ContentPlaceHolder1_btnSearch").click();
}
开发者_JAVA技巧 else return false;
}
//return false;
}
</script>
but not working Please someone help me...
regards
It seems you have KeyPress event handler for a whole document. You should handle KeyPress only for your specific textbox. I.e. you should use different event handlers for masterpage textbox and for content page texboxes. Here is an example using jQuery:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#tbMasterId').keypress(function(e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
$('#ctl00_bluheader1_btnSearch1').click();
return false;
} else {
return true;
}
});
$('#tbContent1Id,#tbContent2Id').keypress(function(e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
$('#ctl00_ContentPlaceHolder1_btnSearch').click();
return false;
} else {
return true;
}
});
});
</script>
精彩评论