2015-07-20 91 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