我有一个文本框,每当用户按下某个键时,都会检查用户是否按下了回车键。如果按下回车键,我想在文本框中添加所有信息并将用户转移到不同的 url。
<script language="javascript" type="text/javascript">
function checkEnter(e){ //e is event object passed from function invocation
var characterCode;
if(e && e.which){ //if which property of event object is supported (NN4)
e = e;
characterCode = e.which; //character code is contained in NN4's which property
}
else{
e = event;
characterCode = e.keyCode; //character code is contained in IE's keyCode property
}
if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
var searchLink = '/Search/?Keywords=' + document.getElementById('<%= searchBox.ClientID %>').value;
transferUser(searchLink);
return false;
}
else{
return true;
}
}
function transferUser(url) {
window.location.href = url;
window.location.replace(url);
}
</script>
Search: <input name="ctl00$searchBox" type="text" id="ctl00_searchBox" class="header_line_search_box_textbox" onKeyPress="checkEnter(event);" />
我已经尝试了所有可能的组合,但没有任何反应。该网站只是刷新自己。
我还需要以某种方式将来自用户的文本转换为安全的 html,必须像 aspx 中的 HttpUtility.EncodeUrl 一样。