Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用React表单更新数组_Javascript_Arrays_Reactjs_Forms_Web - Fatal编程技术网

Javascript 使用React表单更新数组

Javascript 使用React表单更新数组,javascript,arrays,reactjs,forms,web,Javascript,Arrays,Reactjs,Forms,Web,我是初学者,我有一个问题。我想用表单更新一些数组。我有这些数组 state = { loading: true, projects: [], tokens: [] , authors: [] , branches: [] } 然后我有两个选择和一个输入。当用户从select中选择一个值(例如:select“branch”中的值)时,我想将该值添加到他的数组中(在本例中,添加

我是初学者,我有一个问题。我想用表单更新一些数组。我有这些数组

state = {
            loading: true,
            projects: [],
            tokens: [] ,
            authors: [] ,
            branches: [] }

然后我有两个选择和一个输入。当用户从select中选择一个值(例如:select“branch”中的值)时,我想将该值添加到他的数组中(在本例中,添加到“branchs”)。当我完成3个表单元素时,必须有2个按钮

1-“提交”按钮使用当前数组提交表单

2-“添加”按钮将当前值添加到其数组中

                                <div class='forminput'>
                                    <label for="key" id="keyLabel" class="login-label"><strong>Project key</strong></label>
                                    <select id="key"
                                        name="key"
                                        class="login-input"
                                        onChange={this.onChangeProject} required>
                                        {projectsList}
                                    </select>
                                </div>
                                <div class='forminput'>
                                    <label for="branch" id="branchLabel" class="login-label"><strong>Branch key</strong></label>
                                    <select id="branch"
                                        name="branch"
                                        class="login-input" required>
                                        {branchesList}
                                    </select>
                                </div>
                                <div class='forminput'>
                                    <label for="author" id="authorLabel" class="login-label"><strong>Author</strong></label>
                                    <input type="text"
                                        id="author"
                                        name="author"
                                        class="login-input"
                                        maxlength="255"
                                        required
                                        placeholder="Report's author" value={this.state.author}
                                        onChange={this.onChangeAuthor} />
                                    <input type="hidden" name="token" id="token_cnesreport" defaultValue={this.state.token} />
                                </div>
<button type="button" onClick={this.handleAdd}>Add</button>
<input id="generation" name="generation" type="submit" value="Generate" /><br />


项目关键
{projectsList}
分支键
{分支列表}
作者
添加


当用户对表单进行更改时,请尝试保持设置状态,以便在表单完成时,所有数据都已处于该状态

handleInputChange(event){
        this.setState({[event.target.name]:event.target.value})
    }