【发布时间】:2019-01-01 15:40:53
【问题描述】:
这可能是一个广泛的答案,但我会尽量缩短它。我想做的是使用表单从用户那里获取一些信息并使用 JavaScript “验证表单”。如果验证成功,则提交表单。一旦发生这种情况,我将尝试检索使用 java 脚本输入到表单中的数据并显示在同一页面上。(使用模式)
虽然我认为我背后的逻辑是错误的。一旦表单验证它被提交?一旦提交页面刷新?因此输入数据丢失了?这可能是我继续在模态中获取空值的原因吗?
这是表单的html
<div class="feedback-background">
<div class="feedback-content">
<div class="close">+</div>
<img src="../Images/Images2/feedbackimg1.jpg" alt="Givefeedback" style="width:100px;height:100px;">
<form name="FeedbackForm" action="/action_page.php" onsubmit="return validateForm();displayContent();"
method="get">
Name:
<input id="Name" name="Name" type="text" placeholder="Name">
E-Mail:
<input id="E-mail" name="E-mail" type="email" placeholder="E-mail">
What do you think about us?<br>
<textarea id="comment" rows="6" cols="33" name="comment"></textarea>
<br>
How would you rate us ?
<br>
<label><input type="radio" name="rating" id="rating1" value="Excellent" checked>Excellent</label>
<label><input type="radio" name="rating" id="rating2" value="Very Good">Very Good</label>
<label><input type="radio" name="rating" id="rating3" value="Average">Average</label>
<label><input type="radio" name="rating" id="rating4" value="Poor">Poor</label>
<label><input type="radio" name="rating" id="rating5" value="Extreamly Poor">Extremely Poor</label>
<input type="submit" id="submit" value="Submit">
</form>
</div>
这是模式的 HTML,输入数据将再次显示给客户
<div class="popup">
<div class="popuptext" id="myPopup"><p>Thank you <span id="username"></span> ,<br>Your feedback has been
recorded.<br>
<br>You commented that"<span id="usercomment"></span>" <br><br>and rated our website "<span
id="userrating"></span>".
<br><br>Thank you
for taking your time to do so.<br><br>You will now be re-directed automatically</p>
</div>
</div>
这是我用于表单的 css
/*----------comment form----------*/
.feedback-background{
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.7);
position: absolute;
top: 0px;
display: flex;
align-items: center;
justify-content: center;
display:none;
}
.feedback-content{
width: 500px;
height: 550px;
background-color: white;
border-radius: 4px;
padding: 20px;
position:relative;
}
#submit{text-transform:uppercase;
padding:6px 2px;
margin-right: 40px;
margin-top: 20px;
background: #ee0c6e;
font-weight:600;
color:white;
border-radius: 3px;
font-size: 10px;
min-width: 100px;
text-decoration:none
}
input {
width: 50%;
display: block;
margin: 10px 0px;
}
label {
display: block;
}
input[type="radio"]{
width: auto;
display: inline;
}
input[type="submit"]{
width:10em;height: 2em;
cursor:pointer;
}
.close{
position:absolute;
top:0px;
right:14px;
transform:rotate(45deg);
font-size:42px;
cursor:pointer;
}
#feedbacksubmit{
margin-left:600px;
margin-bottom:50px;
background-color:#484848;
border-radius:14px;
padding:10px;
cursor:pointer;
color:white;
outline:none;
}
这是我用于弹出模式的 CSS
/*-----popup----*/
.popup{
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.7);
position: absolute;
top: 0px;
display: flex;
align-items: center;
justify-content: center;
display:none;
}
.popuptext{
width: 100px;
height: 350px;
background-color: #000025;
border-radius: 4px;
padding: 20px;
position:relative;
color:#fff;
width: 20%;
height:30%;
display: block;
margin: 10px 0px;
text-align:center;
margin-left:0px;
margin-bottom:80px;
}
#logo{
margin-right:100px;
float:left;
}
这是我用过的javascripts。
/*------comments form-----*/
document.getElementById('feedbacksubmit').addEventListener('click',
function () {
document.querySelector('.feedback-background').style.display = 'flex';
}
)
document.querySelector('.close').addEventListener('click',
function () {
document.querySelector('.feedback-background').style.display = 'none';
}
)
function displayContent() {
var name = document.getElementById("Name").value;
var comment = document.getElementById("comment").value;
var ratings = document.getElementById('rating');
var rate_value;
for (var i = 0; i < rating.length; i++) {
if (rating[i].checked) {
rate_value = rating[i].value;
}
}
/*these lines are used to write the details into the new modal*/
document.getElementById("username").textContent = name;
document.getElementById("usercomment").textContent = comment;
document.getElementById("userrating").innerHTML = rate_value;
return "";
}
function closePopup() {
document.querySelector('.popup').style.display = 'none';
}
/*-------validation-----*/
function validateForm() {
var x = document.forms["FeedbackForm"]["Name"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
var z = document.forms["FeedbackForm"]["E-mail"].value;
if (z == "") {
alert("Please enter your E-mail address to proceed");
return false;
}
var y = document.forms["FeedbackForm"]["comment"].value;
if (y == "") {
alert("Comment section can not be empty");
return false;
}
var a = 0, rdbtn = document.getElementsByName("rating")
for (i = 0; i < rdbtn.length; i++) {
if (rdbtn.item(i).checked == false) {
a++;
}
}
if (a == rdbtn.length) {
alert("Please select a rating according to you");
return false;
}
document.getElementById('submit').addEventListener('click',
function () {
document.querySelector('.feedback-background').style.display = 'none';
document.querySelector('.popup').style.display = 'flex';
}
)
}
有没有更好的方法?我该怎么做呢?我做错了什么
【问题讨论】:
-
查看stackoverflow.com/a/34347610/3347968,了解如何防止表单提交和页面刷新
-
当一个普通的表单提交时,页面会重新加载来自该表单提交到的端点的响应。当前页面中不存在于持久性介质(例如 cookie 或 Storage)中的所有内容都将被销毁,并替换为新页面。
-
你必须用
event.preventDefault();阻止正常提交才能保持在同一页面,如果你想向端点发送数据,你可以用XHR来做,同时你可以显示你的模态。 -
感谢@Taplar 您的意见,我已考虑到这一点,并已着手进行必要的更改
标签: javascript html