2015-07-20 92 views
0

我想将标签的<input type="text"(和电子邮件)/>display:的边框从none更改为block。 我想我已经做得正确,但也许我没有完全得到onfocus。 下面的代码:
如何在点击提交按钮后使用“display:block”?

var nameSt = false; 
 
var emailSt = false; 
 
var msgSt = false; 
 

 
var name_form = document.forms["mailSender"]["name"].value; 
 
var email_form = document.forms["mailSender"]["email"].value; 
 
var msg_form = document.forms["mailSender"]["message"].value; 
 

 
function namerr() { 
 
    nameSt = true; 
 
    if (emailSt == true && email_form == null && email_form == "") { 
 
    document.getElementById("email_labl").style.display = "block"; 
 
    } 
 
    if (msgSt == true && msg_form == null && msg_form == "") { 
 
    document.getElementById("msg_labl").style.display = "block"; 
 
    } 
 
} 
 

 
function emailerr() { 
 
    emailSt = true; 
 
    if (nameSt == true && name_form == null && name_form == "") { 
 
    document.getElementById("name_labl").style.display = "block"; 
 
    } 
 
    if (msgSt == true && msg_form == null && msg_form == "") { 
 
    document.getElementById("msg_labl").style.display = "block"; 
 
    } 
 
} 
 

 
function msgerr() { 
 
    msgSt = true; 
 
    if (nameSt == true && name_form == null && name_form == "") { 
 
    document.getElementById("name_labl").style.display = "block"; 
 
    } 
 
    if (emailSt == true && email_form == null && email_form == "") { 
 
    document.getElementById("email_labl").style.display = "block"; 
 
    } 
 
}
#name { 
 
    margin-top: 40px; 
 
    width: 300px; 
 
    height: 22px; 
 
    font-family: TikalSansMedium; 
 
    font-size: 12pt; 
 
    border: 1px solid black; 
 
    border-radius: 4px; 
 
    text-align: center; 
 
    transition: all .1s; 
 
} 
 
#name:focus { 
 
    outline: none !important; 
 
    border: 3px solid #F8CB18; 
 
} 
 
#number { 
 
    width: 300px; 
 
    height: 22px; 
 
    font-family: TikalSansMedium; 
 
    font-size: 12pt; 
 
    border: 1px solid black; 
 
    border-radius: 4px; 
 
    text-align: center; 
 
    transition: all .1s; 
 
} 
 
#number:focus { 
 
    outline: none !important; 
 
    border: 3px solid #F8CB18; 
 
} 
 
#email { 
 
    width: 300px; 
 
    height: 22px; 
 
    font-family: TikalSansMedium; 
 
    font-size: 12pt; 
 
    border: 1px solid black; 
 
    border-radius: 4px; 
 
    text-align: center; 
 
    transition: all .1s; 
 
} 
 
#email:focus { 
 
    outline: none !important; 
 
    border: 3px solid #F8CB18; 
 
} 
 
#message { 
 
    resize: none; 
 
    width: 300px; 
 
    height: 100px; 
 
    font-family: TikalSansMedium; 
 
    font-size: 11pt; 
 
    border: 1px solid black; 
 
    border-radius: 4px; 
 
    transition: all .1s; 
 
} 
 
#message:focus { 
 
    outline: none !important; 
 
    border: 3px solid #F8CB18; 
 
} 
 
#name_labl, 
 
#email_labl, 
 
#msg_labl { 
 
    font-family: Arial; 
 
    font-size: 8pt; 
 
    color: red; 
 
    font-weight: bold; 
 
    float: left; 
 
    margin-left: 155px; 
 
    border: 1px solid red; 
 
} 
 
#name_labl { 
 
    display: none; 
 
} 
 
#email_labl { 
 
    display: none; 
 
} 
 
#msg_labl { 
 
    display: none; 
 
}
<form id="mailSender" method="post" action=""> 
 

 
    <div id="nameDiv"> 
 
    <input type="text" id="name" name="name" placeholder="Your Name" onfocus="namerr()"> 
 
    <br> 
 
    <label id="name_labl">This field is required.</label> 
 
    </div> 
 

 
    <div id="numDiv"> 
 
    <input type="text" id="number" name="number" placeholder="Your Mobile Number(Optional)"> 
 
    <br> 
 
    </div> 
 

 
    <div id="mailDiv"> 
 
    <input type="email" id="email" name="email" placeholder="Your Email" onfocus="emailerr()"> 
 
    <br /> 
 
    <label id="email_labl">This field is required.</label> 
 
    </div> 
 

 
    <div id="msgDiv"> 
 
    <textarea id="message" name="message" placeholder="Your Message" class="mCustomScrollbar" data-mcs-theme="minimal-dark" data-mcs-axis="y" onfocus="msgerr()"></textarea> 
 
    <br> 
 
    <label id="msg_labl">This field is required.</label> 
 
    </div> 
 

 
    <br> 
 
    <input type="submit" id="contact_submit" name="contact_submit" value="Send Message"> 
 

 
</form>

+0

使用onblur而不是onfocus(我认为)。 – dandavis

+0

同时添加提交事件中的所有测试,并返回false,如果错误 – mplungjan

回答

1

onfocus将一旦用户点击/选项卡来执行,然后将焦点到给定的元件。你想要的是将onfocus更改为onblur(一旦用户单击/选中另一个元素并保留当前字段,即可执行该操作)。

onfocus:用户将注意力集中在给定的元素上。

onblur:用户“离开”给定的元素。

+0

我会试一试。 但是,由于此代码是现在,它应该不工作? 即使它不是最有效的方法,什么阻止了它的工作? –

+0

当你使用onfocus,并且用户点击输入时,验证会发生,并且此时输入将为空,因为用户只关注该输入。另一种方法是评估oninput,每次用户在输入中写入新字符时都会触发输入。 – taxicala