首先,我是一个超级棒的编码,很抱歉!
在我目前的工作中,我继承了一些超重的联系表格,并在第一次加载着陆页时造成延迟。我一直试图整理一个更干净的版本,这在大多数情况下都很好,但是表单中的下拉列表并不是必需的字段。占位符文本显示我隐藏的“状态”,因为我希望用户需要从下面的列表中进行选择,但是当您提交表单而不选择任何内容时,生成的电子邮件会显示为“状态:状态”
我在这里做错了吗?如果有人有任何建议,我们将不胜感激。
谢谢! 伊恩
@import url(https://fonts.googleapis.com/css?family=Roboto:400,300,600,400italic);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-font-smoothing: antialiased;
-o-font-smoothing: antialiased;
font-smoothing: antialiased;
text-rendering: optimizeLegibility;
}
body {
font-family: "Roboto", Helvetica, Arial, sans-serif;
font-weight: 100;
font-size: 14px;
line-height: 30px;
color: #777;
background: #ffffff;
}
.container {
max-width: 100%;
width: 100%;
margin: 0 auto;
position: relative;
}
#contact input[type="text"],
#contact input[type="email"],
#contact input[type="tel"],
#contact input[type="url"],
#contact select,
#contact textarea,
#contact button[type="submit"] {
font: 400 12px/16px "Roboto", Helvetica, Arial, sans-serif;
}
#contact {
background: #FFFFF;
padding: 0;
margin: 0px 0;
box-shadow: 0;
}
#contact h3 {
display: block;
font-size: 30px;
font-weight: 300;
margin-bottom: 10px;
}
#contact h4 {
margin: 5px 0 15px;
display: block;
font-size: 13px;
font-weight: 400;
}
fieldset {
border: medium none !important;
margin: 0 0 10px;
min-width: 100%;
padding: 0;
width: 100%;
}
#contact input[type="text"],
#contact input[type="email"],
#contact input[type="tel"],
#contact input[type="url"],
#contact select,
#contact textarea {
width: 100%;
border: 1px solid #ccc;
background: #FFF;
margin: 0 0 10px;
padding: 10px;
}
#contact input[type="text"]:hover,
#contact input[type="email"]:hover,
#contact input[type="tel"]:hover,
#contact input[type="url"]:hover,
#contact select:hover,
#contact textarea:hover {
-webkit-transition: border-color 0.3s ease-in-out;
-moz-transition: border-color 0.3s ease-in-out;
transition: border-color 0.3s ease-in-out;
border: 1px solid #aaa;
}
select {
width: 100%;
border: 1px solid #ccc;
background: #FFF;
margin: 0 0 10px;
padding: 10px;
font: 400 12px/16px "Roboto", Helvetica, Arial, sans-serif;
color: #777;
}
#contact textarea {
height: 100px;
max-width: 100%;
resize: none;
}
#contact button[type="submit"] {
cursor: pointer;
width: 100%;
border: none;
background: #CB0000;
color: #FFF;
margin: 0 0 5px;
padding: 15px;
font-size: 15px;
}
#contact button[type="submit"]:hover {
background: #3A3A3A;
-webkit-transition: background 0.3s ease-in-out;
-moz-transition: background 0.3s ease-in-out;
transition: background-color 0.3s ease-in-out;
}
#contact button[type="submit"]:active {
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
}
#contact input:focus,
#contact textarea:focus {
outline: 0;
border: 1px solid #aaa;
}
::-webkit-input-placeholder {
color: #888;
}
:-moz-placeholder {
color: #888;
}
::-moz-placeholder {
color: #888;
}
:-ms-input-placeholder {
color: #888;
}
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<div class="contact">
<form id="contact" method="post" action="/forms/contact-form.php">
<input name="name" placeholder="Full Name: " type="text" tabindex="1" required autofocus>
<input name="email" placeholder="Email: " type="email" tabindex="2" required>
<input name="phone" placeholder="Phone: " type="tel" tabindex="3" required>
<input name="organisation" placeholder="Organisation: " type="text" tabindex="4" required>
<input name="title" placeholder="Position Title: " type="text" tabindex="5" required>
<select name="state" required tabindex="6">
<option hidden="" value="State" disabled selected>State</option>
<option value="NSW">NSW</option>
<option value="VIC">VIC</option>
<option value="QLD">QLD</option>
<option value="WA">WA</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="ACT">ACT</option>
<option value="NT">NT</option>
</select>
<textarea placeholder="Message: " tabindex="7" required></textarea>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</form>
</div>
</div>
</body>
</html>
答案 0 :(得分:0)
您不应该为state选项赋予任何值,以防止用户保留此选择未指定。
所以删除value =“state”就像在下面的代码行中一样:
<option hidden="" value="" disabled selected>State</option>
答案 1 :(得分:0)
不要为<option hidden="" value="State" disabled selected>State</option>
将该值属性设为空。
<option hidden="" value="" disabled selected>State</option>
然后只需要所需的字段。
答案 2 :(得分:0)
使用您的HTML代码,您不得在隐藏,禁用,已选择的选项中放置value
的任何内容。如果required
等于空,value
选项最有效。此外,由于您似乎正在使用HTML5,因此您现在可以自行添加hidden
,类似于您放置disabled
和selected
的方式。因此,您的HTML代码可能是这样的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<div class="contact">
<form id="contact" method="post" action="/forms/contact-form.php">
<input name="name" placeholder="Full Name: " type="text" tabindex="1" required autofocus>
<input name="email" placeholder="Email: " type="email" tabindex="2" required>
<input name="phone" placeholder="Phone: " type="tel" tabindex="3" required>
<input name="organization" placeholder="Organization: " type="text" tabindex="4" required>
<input name="title" placeholder="Position Title: " type="text" tabindex="5" required>
<select name="state" tabindex="6" required>
<option value="" disabled hidden selected>State</option>
<option value="NSW">NSW</option>
<option value="VIC">VIC</option>
<option value="QLD">QLD</option>
<option value="WA">WA</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="ACT">ACT</option>
<option value="NT">NT</option>
</select>
<textarea name="message" placeholder="Message: " tabindex="7" required></textarea>
<button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
</form>
</div>
</div>
</body>
</html>
答案 3 :(得分:0)
是的,您可以在HTML5中使用必需属性来选择Tag。但请记住,第一个值应为空。
<select name="state" required>
<option value="" disabled hidden selected>State</option>
<option value="NSW">NSW</option>
<option value="VIC">VIC</option>
<option value="QLD">QLD</option>
<option value="WA">WA</option>
<option value="SA">SA</option>
<option value="TAS">TAS</option>
<option value="ACT">ACT</option>
<option value="NT">NT</option>
</select>