提示“需要”不显示

问题描述:

什么一段代码“已删除”有关必填字段的提示?提示“需要”不显示

https://jsfiddle.net/venntr/qegxnjm2/

<div class="switch-field"> 
        <input type="radio" id="onlinetak" name="online" required/> 
        <label for="onlinetak">Online</label> 
        <input type="radio" id="onlinenie" name="online" required /> 
        <label for="onlinenie">Offline</label> 
       </div> 
+2

的可能的复制[HTML5必需的属性似乎不工作(http://stackoverflow.com/questions/17966390/html5-required-attribute-seems-不工作的) –

+0

只有当删除CSS它显示,在CSS的东西删除提示:https://jsfiddle.net/venntr/qegxnjm2/2/ – Wiskoza

您的代码可能需要的形式,以便您required工作。

代码:

<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
    <div class="switch-field"> 
     <form> 
      <input id="onlinetak" name="online" type="radio"> <label for="onlinetak">Online</label> <input id="onlinenie" name="online" type="radio"> <label for="onlinenie">Offline</label> <button type="submit">Submit</button> 
     </form> 
    </div> 
</body> 
</html> 

的jsfiddle - https://jsfiddle.net/qegxnjm2/1/

+0

该代码仍然不显示“选择一个选项”弹出。 –

+0

@SheaBelsky,他的问题是:“什么代码”删除了“关于必填字段的提示?”他没有问关于选择一个选项的问题。 –

+0

它不是在'form'标签为是它的一部分,但弹出仍然不能使用,即使它是一个'form'内出现。这与他在输入端设置“display:none”有关。我正在写一个答案。 –

首先要解决的是,它是不是在form标签,正如指出的莱昂。

发生了什么事是你的CSS样式是你的HTML标签,不是你的无线电投入,进行选择。您将输入设置为display: none,因此没有输入的相关错误不会显示。您需要显示输入并编辑输入的样式,以便在没有输入时正确显示错误。

编辑:这是你的代码的例子,将与我所说的工作。

body { 
 
    background-color: #F7FCFF; 
 
    
 
} 
 
.QA radio { 
 
    -webkit-appearance: none; 
 
    background-color: #e1e1e1; 
 
    border: 4px solid #e1e1e1; 
 
    border-radius: 10px; 
 
    width: 100%; 
 
    display: inline-block; 
 
    position: relative; 
 
    width: 20px; 
 
    height: 20px; 
 
    
 
    
 
} 
 
.QA radio:checked { 
 
    background: grey; 
 
    border: 4px solid #e1e1e1; 
 
} 
 

 
input[type=text], textarea { 
 
    -webkit-transition: all 0.30s ease-in-out; 
 
    -moz-transition: all 0.30s ease-in-out; 
 
    -ms-transition: all 0.30s ease-in-out; 
 
    -o-transition: all 0.30s ease-in-out; 
 
    outline: none; 
 
    padding: 3px 0px 3px 3px; 
 
    margin: 5px 1px 3px 0px; 
 
    border: 1px solid #DDDDDD; 
 
} 
 
    
 
input[type=text]:focus, textarea:focus { 
 
    box-shadow: 0 0 5px rgba(81, 203, 238, 1); 
 
    padding: 3px 0px 3px 3px; 
 
    margin: 5px 1px 3px 0px; 
 
    border: 1px solid rgba(81, 203, 238, 1);   
 
} 
 

 
.QA { 
 
    font: normal normal 14px/1 Helvetica; 
 
    margin: 1px; 
 
    border-radius:10px; 
 
    text-align:center; 
 
} 
 

 
.QA1 { 
 
    font: normal normal 14px/1 Helvetica; 
 
    border-radius:10px; 
 
    text-align:left; 
 
} 
 

 
.QA h2{ 
 
    font: normal normal 16px/1 Helvetica;  
 
    font-weight: bold; 
 
    color:#004E97; 
 
} 
 

 
.QA button{ 
 
    text-align:center; 
 
    width: auto; 
 
    background: #E2F4FE; 
 
    font-weight: bold; 
 
    font-size: 14px; 
 
    text-shadow: none; 
 
    color: rgba(0, 0, 0, 0.6); 
 
    border: 1px solid rgba(0, 0, 0, 0.2); 
 
    border-radius: 5px; 
 
    cursor: pointer; 
 
    padding: 6px 14px; 
 
    margin: 2px; 
 
    outline: 0; 
 
     -webkit-transition: all 0.3s ease-in-out; 
 
    -moz-transition: all 0.3s ease-in-out; 
 
    -ms-transition:  all 0.3s ease-in-out; 
 
    -o-transition:  all 0.3s ease-in-out; 
 
    transition:   all 0.3s ease-in-out; 
 
} 
 

 
.QA1 button{ 
 
    text-align:left; 
 
    width: auto; 
 
    background: #E2F4FE; 
 
    font-weight: bold; 
 
    font-size: 14px; 
 
    text-shadow: none; 
 
    color: rgba(0, 0, 0, 0.6); 
 
    border: 1px solid rgba(0, 0, 0, 0.2); 
 
    border-radius: 5px; 
 
    cursor: pointer; 
 
    padding: 6px 14px; 
 
    margin: 2px; 
 
    outline: 0; 
 
     -webkit-transition: all 0.3s ease-in-out; 
 
    -moz-transition: all 0.3s ease-in-out; 
 
    -ms-transition:  all 0.3s ease-in-out; 
 
    -o-transition:  all 0.3s ease-in-out; 
 
    transition:   all 0.3s ease-in-out; 
 
} 
 

 

 
.QA button:hover{ 
 
    box-shadow: 0 0 0 2px white, 0 0 0 3px #27AE60; background: #C1EEFE; 
 
} 
 

 
.QA1 button:hover{ 
 
    box-shadow: 0 0 0 2px white, 0 0 0 3px #27AE60; background: #C1EEFE; 
 
} 
 

 

 
textarea { 
 
background-color: #FFFFFF;  
 
} 
 

 
.index { 
 
z-index: 55;  
 
} 
 

 

 
.switch-field { 
 
    font-family: Helvetica; 
 
    padding: 10px; 
 
    overflow: hidden; 
 
    font-size:0px; 
 
} 
 

 
.switch-title { 
 
    margin-bottom: 6px; 
 
} 
 

 
.switch-field .input-field { 
 
    text-align:center; 
 
    display: inline; 
 
    width: auto; 
 
    background-color: #F2F0F0; 
 
    color: rgba(0, 0, 0, 0.6); 
 
    font-size: 14px; 
 
    font-weight: bold; 
 
    text-shadow: none; 
 
    padding: 6px 14px; 
 
    border: 1px solid rgba(0, 0, 0, 0.2); 
 
    -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); 
 
    -webkit-transition: all 0.3s ease-in-out; 
 
    -moz-transition: all 0.3s ease-in-out; 
 
    -ms-transition:  all 0.3s ease-in-out; 
 
    -o-transition:  all 0.3s ease-in-out; 
 
    transition:   all 0.3s ease-in-out; 
 
} 
 

 
.switch-field label:hover { 
 
    cursor: pointer; 
 
} 
 

 
.switch-field input:checked + label { 
 
    background-color: #5EA8EE; 
 
    -webkit-box-shadow: none; 
 
    box-shadow: none; 
 
} 
 

 
.switch-field .input-field:first-of-type { 
 
    border-radius: 4px 0 0 4px; 
 
} 
 

 
.switch-field .input-field:last-of-type { 
 
    border-radius: 0 4px 4px 0; 
 
} 
 

 
#onlinetak:checked + label:first-of-type { background-color:#5EA8EE; } 
 
#onlinenie:checked + label:last-of-type { background-color:#EE5B5B; }
<form> 
 
    <div class="switch-field"> 
 
    <div class="input-field"> 
 
     <input type="radio" id="onlinetak" name="online" required/> 
 
     <label for="onlinetak">Online</label> 
 
    </div> 
 
    <div class="input-field"> 
 
    <input type="radio" id="onlinenie" name="online" required/> 
 
    <label for="onlinenie">Offline</label> 
 
    </div> 
 
    </div> 
 
    <button type="submit">Submit</button> 
 
</form>

编辑2:这样做的另一个(诚然哈克)的方式是设定一个负z-index上输入自己,让自己不显得标签本身内。错误仍然示出了输入组的附近,但是,输入是“下”的标记(和未设置为display: none),所以错误显示,其中输入是。

.switch-field input { 
    position: absolute; 
    z-index: -10000; 
} 
+0

如何编辑输入看起来? – Wiskoza

+0

这有点复杂。既然你需要显示'输入'和'标签',我会建议把它们放在一个容器中。我在我的答案中添加了一个例子。我现在正在努力使它尽可能地接近你原来的小提琴。 –

+0

我刚刚添加了第二个正是你想要的例子。 –

enter code here 在线 离线