javascript - innerHTML 没有出现
问题描述
我试图收到一条错误消息,如果用户输入的用户名或密码错误,则会在登录框下方弹出一条消息,说明他们输入错误。但由于某种原因,它没有出现。有谁知道为什么?
这是我的代码
const onSubmit = (event) => {
event.preventDefault()
apiClient.signIn(username, password) .then( (response) => {
console.log(response)
if (response === 'Logged In') {
return <Redirect to='/forum' />
}
else {
return document.getElementById('error').innerHTML="Wrong Username or Password"
}
})
.catch(error => {
console.log('Error found when creating meeting');
})
}
return (
<section class="index-banner">
<ThemeProvider theme={theme}>
<Grid container style = {{minHeight: '100vh'}} spacing={0} direction="column" alignItems="center">
<Grid item xs={12} sm={12}>
<div className = "title-2">Sign In</div>
<div>
<FormControl margin = "normal">
<TextField label="Username" value = {username} onChange={e => { getUser(e.target.value) }} spacing={4} />
<TextField label="Password" value = {password} onChange={e => { getPass(e.target.value) }} spacing={4}/>
</FormControl>
</div>
<Grid item xs={12} sm={12}>
<Button onClick={onSubmit} variant="contained" component="span" spacing={4} color = "secondary">
Sign In
</Button>
</Grid>
<Grid item xs={12} sm={12}>
<Button component = {Link} to = "/" spacing={4} color = "primary">
Create Account
</Button>
<div className="error"></div>
</Grid>
</Grid>
</Grid>
</ThemeProvider>
</section>
)
解决方案
value
仅用于或形成诸如input
或textarea
等之类的元素...
else 语句应该是innerHTML
or textContent
:
. . .
else {
return document.getElementById('error').textContent = "Wrong Username or Password"
}
. . .
或者
. . .
else {
return document.getElementById('error').innerHTML = "Wrong Username or Password"
}
. . .
推荐阅读
- javascript - 将 JavaScript 与 WkWebView 一起使用 - UserMessageHandler 上的错误
- python-3.x - 从字符串中提取带逗号的数字
- javascript - 如何在 testcafe cucumberjs 中重新加载页面
- c# - Visual Studio not building InitialTargets or DefaultTargets if project is up to date
- asp.net - 在 URL QueryString 而不是隐藏字段中返回的 cardIdentifier
- python - 使用 Tkinter 为二维数组的每个单元创建矩形
- mysql - 如何查询我的数据库并将其输出到 Google 表格中,以便有人可以编辑此数据,然后将其反映在 MySQL 中?
- strawberry-perl - Perl:如何处理重复的模块安装?
- c++ - OVR 未初始化
- java - 将自定义 @Controller 添加到 Maven 覆盖?