gpt4 book ai didi

javascript - React - 将类添加到空输入字段

转载 作者:行者123 更新时间:2023-11-29 17:35:48 24 4
gpt4 key购买 nike

我只想在输入为空时添加红色边框。我找不到在 React 中“添加类”的方法,所以我正在使用状态。现在代码将为所有输入添加红色边框,即使它有文本。

状态:

this.state = {
inputBorderError: false,
};

HTML/JSX:

<label>Name</label>
<input className={
this.state.inputBorderError ? 'form-input form-input-fail' : 'form-input'
} />

<label>Email</label>
<input className={
this.state.inputBorderError ? 'form-input form-input-fail' : 'form-input'
} />

<label>Message</label>
<textarea className={
this.state.inputBorderError ? 'form-input form-input-fail' : 'form-input'
} />

CSS:

.form-input-fail {
border: 1px solid red;
}

JS:

   let inputFields = document.getElementsByClassName('form-input');

for (var i = 0; i < inputFields.length; i++) {
if (inputFields[i].value === '') {
this.setState({
inputBorderError: true,
});
}
}

我在我的代码中看到错误,因为它基本上是在发现空输入时将状态设置为 true。我想我可能会错误地处理这个问题,因为只有一个状态。是否有基于我的状态方法的解决方案,或者是否有其他解决方案?

最佳答案

现在,您只有一个影响所有输入的状态值,您应该考虑为每个输入设置一个。此外,您的输入不受控制,将更难记录和跟踪它们的值以进行错误处理。

给每个 input 标签一个名称属性是个好习惯。使其更容易动态更新其相应的状态值。

尝试如下操作,开始在每个输入中输入内容,然后删除文本:https://codesandbox.io/s/nervous-feynman-vfmh5

class App extends React.Component {
state = {
inputs: {
name: "",
email: "",
message: ""
},
errors: {
name: false,
email: false,
message: false
}
};

handleOnChange = event => {
this.setState({
inputs: {
...this.state.inputs,
[event.target.name]: event.target.value
},
errors: {
...this.state.errors,
[event.target.name]: false
}
});
};

handleOnBlur = event => {
const { inputs } = this.state;
if (inputs[event.target.name].length === 0) {
this.setState({
errors: {
...this.state.errors,
[event.target.name]: true
}
});
}
};

handleOnSubmit = event => {
event.preventDefault();
const { inputs, errors } = this.state;
//create new errors object
let newErrorsObj = Object.entries(inputs)
.filter(([key, value]) => {
return value.length === 0;
})
.reduce((obj, [key, value]) => {
if (value.length === 0) {
obj[key] = true;
} else {
obj[key] = false;
}
return obj;
}, {});

if (Object.keys(newErrorsObj).length > 0) {
this.setState({
errors: newErrorsObj
});
}
};

render() {
const { inputs, errors } = this.state;
return (
<div>
<form onSubmit={this.handleOnSubmit}>
<label>Name</label>
<input
className={
errors.name ? "form-input form-input-fail" : "form-input"
}
name="name"
value={inputs.name}
onChange={this.handleOnChange}
onBlur={this.handleOnBlur}
/>

<label>Email</label>
<input
className={
errors.email ? "form-input form-input-fail" : "form-input"
}
name="email"
value={inputs.email}
onChange={this.handleOnChange}
onBlur={this.handleOnBlur}
/>

<label>Message</label>
<textarea
className={
errors.message ? "form-input form-input-fail" : "form-input"
}
name="message"
value={inputs.message}
onChange={this.handleOnChange}
onBlur={this.handleOnBlur}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}

关于javascript - React - 将类添加到空输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56656816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com