【发布时间】:2014-06-19 02:14:26
【问题描述】:
我的render 函数中有一个简单的表单,如下所示:
render : function() {
return (
<form>
<input type="text" name="email" placeholder="Email" />
<input type="password" name="password" placeholder="Password" />
<button type="button" onClick={this.handleLogin}>Login</button>
</form>
);
},
handleLogin: function() {
//How to access email and password here ?
}
我应该在我的handleLogin: function() { ... } 中写什么来访问Email 和Password 字段?
【问题讨论】:
-
注意:您应该在表单上处理
onSubmit,而不是单击按钮 - 这样您还将处理用户通过按 Enter 提交表单。 -
当用户在表单的任何
<input type=text>中按 Enter 键时,将提交带有<button>的<form>或带有type=submit的<input>。如果您依赖一个按钮的onClick,用户必须单击该按钮或将其聚焦并按 Enter/空格键。使用onSubmit将启用这两个用例。当表单不支持 Enter 提交时,他们会感到崩溃。 -
浏览器原生检查必填字段的空值也有好处
标签: javascript html reactjs frontend