我有一个搜索文本框,它有一些限制,它不应该允许特殊字符,但是在单击enter键时,它应该重定向到相应搜索的搜索页面。我已经为regex添加了不允许特殊字符的代码,但是当用户键入搜索时,应该如何实现它应该重定向到enter的正确搜索页面。
我的结果搜索页面名是search.aspx
。请参阅JS代码以供参考。
$(document).ready(function (){
$('#ctl00_topNavigation_txtSearch').bind('keypress', function (event) {
var regex = new RegExp("^[a-zA-Z0-9\b _ _%]+$");
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!regex.test(key)) {
event.preventDefault();
return false;
}
});
});
发布于 2014-06-26 00:34:16
在你的按键事件中试试这个
var searchUrl = "search.aspx?tx=" + ctl00_topNavigation_txtSearch.val();
window.location.replace(searchUrl);
发布于 2014-06-25 22:36:05
enter的键码为13,所以只需添加一个检查是否Keycode为13,如果是,则重定向用户。
function (event) {
if (event.which == 13 || event.keyCode == 13) {
//code to redirect goes here
//window.location.href = "http://stackoverflow.com";
return false;
}
return true;
});
https://stackoverflow.com/questions/24423841
复制