- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个这样的:
使用以下代码:
<form onKeyPress={this.login.bind(this)}>
<input type="text" placeholder="username"/>
<input type="password" placeholder="password"/>
<button type="button" onClick={this.login.bind(this)}>Log In</button>
</form>
虽然我有如下所示的 login()
方法:
login(e){
if((e.type==='keypress' && e.which===13) || e.type==='click'){
//Do login operations:
this.props.store.login()//the method inside MobX store
}
}
在以下情况下,没有错误,我可以登录:
login
按钮但是下面的第三种情况,由于登录操作被调用两次而给我错误:
我想知道可以避免第三种情况错误的最佳做法是什么。我查看了其他相关的 SO 问题,但我无法找出最佳做法。
我忘了说我正在使用 ReactJS 和 MobX。
最佳答案
通过移动 onKeyPress
解决了问题来自 <form>
的属性标记为文本类型 <input>
标签:
<form>
<input type="text" placeholder="username" onKeyPress={this.login.bind(this)}/>
<input type="password" placeholder="password" onKeyPress={this.login.bind(this)}/>
<button type="button" onClick={this.login.bind(this)}>Log In</button>
</form>
关于javascript - 冲突事件 : onKeyPress & onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43303980/
function h(x) { alert(x); } 当我按“a”警报为空时 当我在“a”之后按“b”=>ab 只提醒“a”并且我想要“ab” 当我输入“abcd”时,它只提示“abc”,
我正在编写一个简单的 javascript 表单,用于根据值“blue”检查输入值。现在,如果您输入“blue”,它会说它不正确,但如果添加任何其他字符,它会说正确。好像有一个字符的延迟,所以当我输入
我有如下 HTML 文本输入 ID: = 48 && event.charCode 现在,在 onkeypress 中我成功添加了,因此只能输入数字。但我有一个 JavaSc
我正在使用以下内容并尝试让用户的字母猜测出现在屏幕上,但是当我检查开发工具时,按键将返回到数组,如 [ , , , , , ,] 。没有字母。有想法吗? var letguessText = docu
我有一个可选电话号码的 html 输入,以及 2 个 radio 输入(留言、不留言): #session.secondaryPhone#" /> 请注意,我正在运行两个事件(删除其中一个事件会产生相
我有一个项目列表,上面有一个输入字段。 输入字段是一个过滤器,它应该根据您在输入字段中键入的文本过滤列表。 例如:如果您输入“th”,它应该过滤列表,以便所有项目都以“th”开头。 为此,我使用 Aj
我编写了一个处理鼠标和键盘事件的代码。但是 onkeypress 事件在 Firefox、IE 等中没有触发。我也尝试过 keydown 事件,但这不起作用。 li 元素的 HTML 代码为: pri
我正在学习 Javascript.. 仅当我的脚本标记位于输入文本元素下方时,下面的代码才起作用,但如果我将代码放置在输入文本标记上方,则它不起作用。我可以知道为什么吗?下面是代码:
我正在尝试使用 JavaScript 将 onKeyPress 事件添加到动态创建的 html 元素中。实际上,添加了元素,分配了 id,并添加了 innerHTML,但是 onmouseover、o
我有一个关键字搜索文本框,我想在用户按下 Enter 键时运行搜索功能。但是现在该事件不会被解雇。我的代码如下: onkeyPress=(e)=>{ if(e.key === "Enter" |
如果未选中复选框,我将使用 JavaScript 禁用键盘,如果选中复选框,则仅启用数字。 我的代码是。 function isNumberKey(evt) { i
document.getElementById('my').onkeypress = function(event) { this.value = this.value.toUpperCa
我的 onkeypress 事件在我网站的搜索栏上不起作用。这是我的 javascript 和 html 代码: window.onload = function() { document.g
我正在 try catch 窗口上的按键事件(使用使用 Gecko 引擎的应用程序打开的 html 页面) function onkeypress(){ alert("key presse
我要求不使用 html 事件处理程序。所以我试图从 script 处理它。我做了一个小功能,但它不起作用 function lld () { document.getElementById
使用 Javascript 的 onKeypress 尝试在返回后调用函数。这不起作用,因为由于验证数字,该字段尚未完成。 onKeyPress="return numbersonly(event,
对于下面的文本框: 这个脚本运行良好。 function doit_onkeypress(event){ if (event.keyCode == 13 || event.which
这段代码有以下问题: search();如果我按下一个键就不会被解雇。但是当我删除 一切正常。 有什么方法可以解雇onkeypress在表单或任何其他函数中? 我正在使用 Phon
每次在文本框中按下一个键时,我都需要调用一个 js 函数: function checkValidity() { alert("Checking..
我有 5 个可点击的 div 标签,div 显示一个带有数字的黄色方 block 。当用户点击一个方 block 时,方 block 的颜色会发生变化,表明被点击的方 block 是“事件”方 blo
我是一名优秀的程序员,十分优秀!