gpt4 book ai didi

javascript - 控制台错误: "Form submission canceled because the form is not connected"

转载 作者:行者123 更新时间:2023-12-01 03:23:06 25 4
gpt4 key购买 nike

我收到控制台错误:“表单提交已取消,因为表单未连接”

根据this thread我需要在某处添加以下行:

document.body.appendChild(form);

作为 React.js 的新手,我尝试将其放置在主组件的渲染函数内部及其上方。两人都打破了页面。我还尝试将其添加到 index.js 文件中,但这也破坏了页面。

如果这是基于react.js 的表单的正确代码,那么它应该准确放置在哪里?

Usurvey.js(包含 2 个表单,首次加载时出现 1 个错误)

import React, { Component } from 'react'

const firebase = require('firebase')
const shortid = require('shortid')

// Initialize Firebase
var config = {
apiKey: "[removed]",
authDomain: "[removed]",
databaseURL: "[removed]",
projectId: "[removed]",
storageBucket: "[removed]",
messagingSenderId: "[removed]"
};
firebase.initializeApp(config);

class Usurvey extends Component {

handleNameSubmit(event) {
var nameValue = this.refs.nameRef.value
this.setState({
studentName: nameValue
}, function() {
// console.log(this.state)
})
}

handleAnswerSelect(e) {
// var answers = e.target.value
let answers = this.state.answers

if (e.target.name === 'answer1') {
answers.answer1 = e.target.value
} else if (e.target.name === 'answer2') {
answers.answer2 = e.target.value
} else if (e.target.name === 'answer3') {
answers.answer3 = e.target.value
}
this.setState({
answers: answers
}, function () {
console.log(this.state)
})
}

handleQuestionSubmit () {
firebase.database().ref('uSurvey/'+this.state.uid).set({
studentName: this.state.studentName,
answers: this.state.answers
})
this.setState({
isSubmitted: true
})
}


constructor(props) {
super(props)

this.state = {
uid: shortid.generate(),
studentName: '',
answers: {
answer1: '',
answer2: '',
answer3: ''
},
isSubmitted: false
}
this.handleNameSubmit = this.handleNameSubmit.bind(this);
this.handleAnswerSelect = this.handleAnswerSelect.bind(this)
this.handleQuestionSubmit = this.handleQuestionSubmit.bind(this)
}

render() {
let nameHTML
let questionsHTML

if (this.state.studentName === '' && this.state.isSubmitted === false) {
nameHTML = (
<div>
<h1>Hi, what is your name?</h1>
<form className="formTag" id="form1" onSubmit={this.handleNameSubmit}>
<input className="form-input" id="test-id" type="text" placeholder="Enter your name" ref="nameRef" />
</form>
</div>
)
questionsHTML = ''
} else if (this.state.studentName !== '' && this.state.isSubmitted === false) {
// nameHTML = <h1>Thanks for filling out our form, {this.state.studentName}</h1>
questionsHTML = (
<div>
<h2>Questions</h2>
<form className="formTag" id="form2" onSubmit={this.handleFormSubmit}>
<div className="card">
<label htmlFor="">What courses do you prefer?</label>
<br/>
<input type="radio" name="answer1" value="Technology" onChange={this.handleAnswerSelect}/> Technology
<input type="radio" name="answer1" value="Design" onChange={this.handleAnswerSelect}/> Design
<input type="radio" name="answer1" value="Marketing" onChange={this.handleAnswerSelect}/> Marketing
</div>

<div className="card">
<label htmlFor="">What is your Age?</label>
<br/>
<input type="radio" name="answer2" value="<20>" onChange={this.handleAnswerSelect}/> 20 or less
<input type="radio" name="answer2" value="21-30" onChange={this.handleAnswerSelect}/> 21-30
<input type="radio" name="answer2" value="30+" onChange={this.handleAnswerSelect}/> 30+
</div>

<div className="card">
<label htmlFor="">What is your learning objective?</label>
<br/>
<input type="radio" name="answer3" value="Freelance" onChange={this.handleAnswerSelect}/> Freelance
<input type="radio" name="answer3" value="Front-End" onChange={this.handleAnswerSelect}/> Front-End
<input type="radio" name="answer3" value="Back-End" onChange={this.handleAnswerSelect}/> Back-End
</div>

<input className="feedback-button" type="submit" value="submit" />
</form>
</div>
)

} else if (this.state.studentName !== '' && this.state.isSubmitted === true) {
nameHTML = <h1>Thanks for submitting our survey, {this.state.studentName}</h1>
}
return (
<div>
{nameHTML}
{/*------------------*/}
{questionsHTML}
</div>
)
}
}

export default Usurvey

最佳答案

您可以在handleNameSubmit函数上添加event.preventDefault();

 handleNameSubmit(event) {
var nameValue = this.refs.nameRef.value
this.setState({
studentName: nameValue
}, function() {
// console.log(this.state)
})

event.preventDefault(); // add event.preventDefault();
}

关于javascript - 控制台错误: "Form submission canceled because the form is not connected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45024214/

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