- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
假设我们有一个 onKeyUp
处理程序:
handleKeyUp: function(e) {
/* handle stuff */
},
我们有几个输入组件,它们都可以触发处理程序:
<input type="text" ref="login" onKeyUp={this.handleKeyUp} />
...
<input type="text" ref="pwd" onKeyUp={this.handleKeyUp} />
如何让处理程序检测 onKeyUp
是从 login
还是 pwd
触发的?
一个场景是我在 pwd
上检测到 Tab 键按下,然后我继续尝试保存文本字段(但不是我从 login
tab 关闭的地方)。
我已经尝试查看 e.target
的详细信息,但无法弄清楚如何引用原始组件。
更新
对不起,一定是没想清楚。是的,e.target
是对原始组件的引用。我一直在寻找 ref
的句柄来获取值。但我不需要 ref,我可以从 e.target.value
中获取值。
最佳答案
如 React's Event System
documentation 中所述:
Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.
因此,SyntheticEvent
的一个实例被传递给您的回调
handleKeyUp: function(event) {
/* event is an instance of SyntheticEvent
from wich you can extract the currentTarget
*/
},
编辑:如果你真的想在做任何事情之前访问组件的 ref
名称,这里是你如何在 ES6 中做到这一点:
class MyComponent extends React.Component {
constructor() {
super();
this.handleLoginKeyUp = this.keyUpHandler.bind(this, 'LoginInput');
this.handlePwdKeyUp = this.keyUpHandler.bind(this, 'PwdInput');
}
keyUpHandler(refName, e) {
console.log(refName);
// prints either LoginInput or PwdInput
}
render() {
return (
<div>
<input type="text" onKeyUp={this.handleLoginKeyUp} ref="LoginInput" />
<input type="text" onKeyUp={this.handlePwdKeyUp} ref="PwdInput" />
</div>
);
}
}
关于javascript - 如何检测哪个 React 组件触发了 onKeyUp 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35862979/
关于如何禁用 MVC3 远程验证器具有的 onKeyup 设置,我看到了很多答案。我看到的回复是输入这个: $.validator.setDefaults({ onkeyup: false }); 但
所以我正在为历史课制作模拟版本的谷歌审查制度。我试图让搜索在按回车键后开始工作,但到目前为止,我得到的只是对我输入的第一个字母的搜索。有什么建议么? HTML代码: Jav
据说 onkeyup 方法没有定义,但是,该方法是我的 ide 自动推荐给我的。当我在 chrome 开发工具中查看错误时,出现错误 Uncaught TypeError: Object [objec
这个问题在这里已经有了答案: How to delay the .keyup() handler until the user stops typing? (28 个回答) 关闭6年前. 我想创建一个
我有一堆 HTML 数字输入,我已经通过以下方式获取了它们 x=document.querySelectorAll('input[type="number"]'); 然后,我尝试使用 for 循环对其
我有一些输入字段,它们使用 onKeyUp="script" 在输入内容时返回数据。 作为一种快捷方式,我希望能够在从另一个位置输入数据并触发脚本时向其中一个字段添加值。 我可以使用 document
出于某种原因,对于 IE9 上的某些人来说,我的 onkeyup 似乎被触发了两次。 我当前的函数如下所示: document.getElementById("chatboxtextarea").on
我创建脚本在 keyup 上按出生日期获取年龄,但是 我有个问题。因为,当我打字时 示例:我输入:1955-11-13 此结果显示 NaN 这是我的脚本: $(document).read
jquery中有没有等于onkeyup=""的函数 我已经找到了change(),但是这只在模糊时执行警报,我希望它像实时搜索一样,当你写“a”(想要写anton)时,它应该执行该函数。所以当你写5时
在阅读了一堆问题并对其应用“解决方案”后,我发现 jQuery 验证器未验证 onkeyup。默认情况下,应该如此。我通过下载新文件来确保源代码没有被修改,什么都没有。 版本:jquery 1.10.
我在 jquery 函数中有以下 if 语句,由文本输入字段中的 onKeyUp 触发 if ( famcount 10 ){ alert('number too large.'); } 问
我有一个 ExtJS 组合框,这是一个必填字段,但我不希望在我尝试提交表单之前显示必填字段错误消息。现在,它似乎发生在 keyup 上。我已将 validateOnBlur 设置为 false,但它仍
我的预期输出是 当我在输入文本字段中输入时,我会在第三个文本字段上得到即时总和 如果我在第一个文本字段中输入 3 并且按键已按下,则 3+0=3 必须立即显示在第三个字段上 但没有显示任何结果
我知道这个问题已经被问了很多次 - 但我找到的所有答案都很旧,而且似乎不起作用(至少对我来说)。 我使用以下代码从输入框进行实时预览 $('#attractionName').on('keyup',
我需要在 keyup 和 onsubmit 上进行验证。我有一个名为 CVV 的字段,它接受 3 位或 4 位数字,所以我使用的是“^(d){3,4}$”。当我在 onsubmit 函数中执行此模式时
我正在尝试确定用户按下某个键后的时间量(秒)。这是一个输入框的值。 我想在文本框的值发生变化时实用地调用 updateData 函数,但仅在 onKeyUp 的特定持续时间之后。 我目前正在检查 va
我有一个包含许多动态插入范围的 contentEditable 字段。 当我使用 onclick() 事件中的 getTarget() 时,我将目标作为相应的跨度,但使用 onkeyup() 和 on
到目前为止我做得很好,但我无法将其实现到 html 对象中。 http://jsfiddle.net/fkyk2b8y/48/ 假设我有一个像 这样的 html 元素我想这样实现: function
我有一个供人们输入用户名的输入文本框,它有一个附加的 onkeyup 事件属性,它会触发 AJAX 请求以检查用户名是否已被占用。 第一个键到第二个键之间的延迟是多少?意思是如果我一个接一个地输入一个
我似乎没有找到哪个版本的“onkeyup”是正确的: 驼峰式:onKeyUp 或 小写:onkeyup [更新] http://www.w3.org/TR/html4/intro/sgmltut.ht
我是一名优秀的程序员,十分优秀!