javascript 简单的 HTML 输入 onkeypress 事件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12095875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple HTML input onkeypress event not working
提问by rayvd
I'm new here (and to web development in general), and I have been trying to understand why my function is not being executed on the specified event.
我是新来的(以及一般的 Web 开发),我一直在试图理解为什么我的函数没有在指定的事件上执行。
I found this post, and it seems exactly like what I want, but even this did not work:
我找到了这篇文章,它看起来和我想要的完全一样,但即使这样也不起作用:
html <input type="text" /> onchange event not working
html <input type="text" /> onchange 事件不起作用
Any help would be appreciated. My exact code follows. I have some text input fields (actually search boxes), and ultimately I want to have it check a checkbox when the user enters data into the text fields, but it doesn't even seem to call the function.
任何帮助,将不胜感激。我的确切代码如下。我有一些文本输入字段(实际上是搜索框),最终我想让它在用户在文本字段中输入数据时选中一个复选框,但它似乎甚至没有调用该函数。
I have tried a few variants while reading the post mentioned above. Here are some input field attributes I have tried:
在阅读上面提到的帖子时,我尝试了一些变体。以下是我尝试过的一些输入字段属性:
<input type="date" name="dt" size="10" value="2012-07-21" onChange="SetCheckBox('d')" />
<input type="search" size="10" name="sl" value="" onChange="SetCheckBox('n')" />
<input type="search" size="10" name="sf" value="" onkeypress="SetCheckBox('n')" />
<input type="search" size="20" name="st" value="" onkeypress="SetCheckBox(this);" />
and here is my javascript:
这是我的javascript:
<script language="javascript" type="text/javascript">
Function SetCheckBox(id) {
alert (id.value);
document.write('test');
}
</script>
I have tried not passing any arguments and just doing a document.write, but it doesn't seem to be calling the function. And yes, javascript is enabled and working elsewhere on the page just fine! The script is in the body, just below the form.
我试过不传递任何参数,只做一个 document.write,但它似乎没有调用该函数。是的,javascript 已启用并且可以在页面上的其他地方正常工作!脚本位于正文中,就在表单下方。
The (lack of) behavior is the same in multiple browsers. Thank you for any help you can provide. Ray
(缺乏)行为在多个浏览器中是相同的。感谢您提供任何帮助。射线
回答by Gabe
For javascript the function
keyword is lowercase.
对于javascript,function
关键字是小写的。
Function SetCheckBox(id)
needs to be:
需要是:
function SetCheckBox(id)
Also, you're not passing object to get an id, so...
另外,您不是通过对象来获取 id,所以...
function SetCheckBox(id) {
var ele = document.getElemenyById(id);
alert (ele.value);
document.write('test');
}
回答by mplungjan
Several issues apart from the already mentioned uppercase F in Function
除了已经提到的Function中的大写F之外的几个问题
- your function passes a variable called id but expects a field
- you pass a string that is not an ID and not referring to a field
- only the last version using (this) will work, but there is no value to alert
- document.write will WIPE the page and all scripts on it when it is invoked after page load (e.g. not inline)
- 您的函数传递一个名为 id 的变量,但需要一个字段
- 您传递一个不是 ID 且不引用字段的字符串
- 只有使用 (this) 的最后一个版本才有效,但没有提醒价值
- document.write 将在页面加载后调用时擦除页面及其上的所有脚本(例如非内联)
So code should be EITHER
所以代码应该是
function SetCheckBox(id) {
var val = document.getElementById(id).value
alert (val);
document.getElementById('someContainer').innerHTML=val;
}
OR
或者
function SetCheckBox(fld) {
var val = fld.value
alert (val);
document.getElementById('someContainer').innerHTML=val;
}
Based on your description, my guess is you want to do this: DEMO
根据您的描述,我猜您想这样做:DEMO
<input type="date" id="dt" name="dt" size="10" value="2012-07-21"
onkeypress="SetCheckBox(this)" />
<input type="checkbox" id="dtChk" />
using this script
使用这个脚本
function SetCheckBox(fld) {
var checkbox = document.getElementById(fld.id+"Chk");
// check if something is entered, uncheck if not
checkbox.checked=fld.value.length>0;
}
and maybe even with this addition
甚至可能加上这个
window.onload=function() {
document.getElementById("dt").onkeypress();
}
which will check the box if the field is not empty at (re)load time
如果该字段在(重新)加载时不为空,它将选中该框
回答by user1619962
onkeypress and onchange event should works
onkeypress 和 onchange 事件应该有效
<body>
<script type="text/javascript">
function SetCheckBox() {
alert("1");
}
</script>
<input id = "test" value="" onkeypress="SetCheckBox();" />
<input id = "test1" value="" onchange="SetCheckBox()" />
</body>
回答by Anoop
in javascript function keyword is written in small letters and here you wrote F in caps.
在 javascript 中,function 关键字用小写字母写,在这里你用大写字母写 F。