使用原型 - 如何观察回车的输入字段

Using Prototype- how do I watch input field for carriage return?

本文关键字:观察 回车 字段 输入 原型 何观察      更新时间:2023-09-26

我正在使用JS库原型 - 如何观察文本输入字段,并在按下返回时向用户发出警报字段的内容?(页面上没有<表单>,只有输入字段。

大致是我想做的:

Enter something: <input type='text' id='myfield'>
<script>
  if([keydown event in #myfield]) {
     alert("You typed: "+[contents of #myfield]);
  }
</script>

谢谢。

$('myfield').observe('keypress', function(event){
    if(event.keyCode == Event.KEY_RETURN)
        alert($('myfield').value);
});​

你可以在这个jsfiddle中找到一个简单的演示

查看 http://prototypejs.org/api/event

如果你举个例子,你可能会得到更好的答案。

为什么不监视submit

对于元素<input id="signinForm"/>

Event.observe('signinForm', 'submit', checkForm);
function checkForm() { //do something }`

有关详细信息,请参阅此处的文档:

http://prototypejs.org/api/event/observe