Javascript 每次按下回车键时,我都想提醒一些事情
每次当我按下输入框中的enter键时,它都会提醒一些事情,但在这样做时会遇到问题。下面是代码Javascript 每次按下回车键时,我都想提醒一些事情,javascript,Javascript,每次当我按下输入框中的enter键时,它都会提醒一些事情,但在这样做时会遇到问题。下面是代码 <input type="text" class="searchfld" id='input' onchange="gotothatpost(this.value)" onkeyup="ajxsrch(this.value)"> 下面是js代码 <script> function ajxsrch(str) { var keycod; if(window.e
<input type="text" class="searchfld" id='input' onchange="gotothatpost(this.value)" onkeyup="ajxsrch(this.value)">
下面是js代码
<script>
function ajxsrch(str)
{
var keycod;
if(window.event)
{
keycod = str.getAscii();
}
if(keycod==13){alert("You pressed Enter");}
}
</script>
函数ajxsrch(str)
{
var-keycod;
if(window.event)
{
keycod=str.getAscii();
}
如果(keycod==13){alert(“您按下了Enter”);}
}
试试这个
<input type="text" class="searchfld" id='input' onchange="gotothatpost(this.value)" onkeyup="ajxsrch(event)">
<script>
function ajxsrch(e)
{
if (e.which === 13) {
alert("You pressed Enter");
}
return false;
}
</script>
函数ajxsrch(e)
{
如果(e.which==13){
警报(“您按下回车键”);
}
返回false;
}
我认为这是因为您没有将e传递给函数,而只使用window.event,这在所有浏览器中都不起作用。请尝试此代码
<input type="text" class="searchfld" id='input' onchange="gotothatpost(this.value)">
<script>
function ajxsrch(e)
{
e = e||event;
var keycod;
if(e)
{
keycod = e.keyCode||e.which;
}
if(keycod==13){alert("You pressed Enter");}
}
document.getElementById("input").onkeyup=ajxsrch;
</script>
函数ajxsrch(e)
{
e=e | |事件;
var-keycod;
如果(e)
{
keycod=e.keyCode | | e.which;
}
如果(keycod==13){alert(“您按下了Enter”);}
}
document.getElementById(“输入”).onkeyup=ajxsrch;
将事件对象传递给函数调用
<input type="text" class="searchfld" id='input' onkeyup="ajxsrch(event)">
出什么事了?没有警报?还有什么吗?在你的js代码中,ajxsech应该是ajxsrchyeah,不显示alert@trevor现在忽略这一点什么是
getAscii()
方法?还有,为什么不使用onkeypress方法,然后从事件中获取密钥?这毫无意义。你把e
放入事件处理程序,然后你就再也不能从中得到keycode了。在这个例子中,变量名keycod有点误导人。此人正在使用函数getAscii,该函数获取上次按下的键的ASCII代码,因此OP的操作方式是有效的。可能最好使用e.which或e.keyCode或e.charCode等,但为了尽可能接近OP的代码,这也可以。e
与此处的任何内容有什么关系?字符串值上的getAscii()
文档在哪里?即使用e,Firefox/Chrome使用window.event。这就是e与任何事情的关系。另外,我的缺点是,我正在查看的文档是ActionScript:S,现在已修复。有一段时间我以为我刚刚学会了一个新功能:POK,现在你实际上在使用事件中的键码。最后,这个答案有点道理。奇怪的是,OP在它根本不起作用的时候接受了它。
function ajxsrch(ev) {
var ch = ev.keyCode || ev.which || ev.charCode; // Proper way of getting the key value
if(ch == 13) {
alert("You pressed enter");
}
}