2014-02-06 3 views
0

사용자가 내 사이트의 텍스트 상자에 초점을 맞출 때 JavaScript 파일을 내보내는 방법을 찾고 있습니다.텍스트 상자에 초점이 맞지 않으면 JavaScript 파일을 저장하는 방법은 무엇입니까?

배경 정보 :

이 사용자 "비밀번호"가 필드 일치 "비밀번호 확인"있는지 확인하는 가입하기 양식입니다. 지금까지 작성한 스크립트는 사용자가 가입 데이터를 제출하려고 할 때 텍스트 상자의 테두리를 모두 빨간색으로 표시하지만 비밀번호가 일치하지 않습니다.

나는 일이 원하는 것은 : 내가 대신 텍스트 상자의 경계

을 사용자가 양식을 제출하려고 후 암호가 일치하지 않는 경우 빨간색 회전, 나는 때이 스크립트 불을 가지고 싶습니다 사용자는 "암호 확인"텍스트 상자에 초점을 맞추지 않으며 암호가 일치하지 않습니다.

는 는

내 코드 :

는 HTML :

<div id="FormInput3"> 

    <h3>Create Password</h3> 

    <input id="pass1" class="signup-textboxes" placeholder="Enter your desired password..." type="password" onfocus="emptyElement('status')" maxlength="100"> 

</div> 

<div id="FormInput4"> 

    <h3>Confirm Password</h3> 

    <input id="pass2" class="signup-textboxes" placeholder="Confirm your password..." type="password" onfocus="emptyElement('status')" maxlength="100"> 

</div> 

<button id="signupbtn" onclick="signup()">Create Account</button> 

는 CSS :

/* ****Password 1**** */ 

#signupform > #FormInput3 { 

    width: 450px; 

    height: 40px; 

    line-height: 40px; 

    margin-left: 20px; 

    display: block; 

    margin-left: auto; 

    margin-right: auto; 

} 

#signupform > #FormInput3 h3 { 

    vertical-align: middle; 

    font-family: "Lato Light", Arial; 

    color: gray; 

    font-size: 20px; 

    font-family: "Lato Regular", Arial; 

    font-weight: 100; 

    margin-left: 24px; 

} 

#signupform > #FormInput3 > #pass1{ 

    float: right; 

    color: gray; 

    margin-top: -60px; 

    width: 250px; 

    background-color: #000; 

    border: none; 

    height: 30px; 

    margin-right: 5px; 

    border-bottom: 1px dotted gray; 

    font-size: 15px; 

    outline: none; 

    -webkit-transition: border-color 0.3s ease; 

    -moz-transition: border-color 0.3s ease; 

    -ms-transition: border-color 0.3s ease; 

    transition: border-color 0.3s ease; 

} 

#signupform > #FormInput3 > #pass1:hover { 

    border-color: #fff; 

    color: white; 

} 

#signupform > #FormInput3 > #pass1:focus{ 

    border-color: #fff; 

    color: white; 

} 

/* ****Password 2**** */ 

#signupform > #FormInput4 { 

    width: 450px; 

    height: 40px; 

    line-height: 40px; 

    margin-left: 20px; 

    display: block; 

    margin-left: auto; 

    margin-right: auto; 

} 

#signupform > #FormInput4 h3 { 

    vertical-align: middle; 

    font-family: "Lato Light", Arial; 

    color: gray; 

    font-size: 20px; 

    font-family: "Lato Regular", Arial; 

    font-weight: 100; 

    margin-left: 10px; 

} 

#signupform > #FormInput4 > #pass2 { 

    float: right; 

    color: gray; 

    margin-top: -60px; 

    width: 250px; 

    background-color: #000; 

    border: none; 

    height: 30px; 

    margin-right: 5px; 

    border-bottom: 1px dotted gray; 

    font-size: 15px; 

    outline: none; 

    -webkit-transition: border-color 0.3s ease; 

    -moz-transition: border-color 0.3s ease; 

    -ms-transition: border-color 0.3s ease; 

    transition: border-color 0.3s ease; 

} 

#signupform > #FormInput4 > #pass2:hover { 

    border-color: #fff; 

    color: white; 

} 

#signupform > #FormInput4 > #pass2:focus{ 

    border-color: #fff; 

    color: white; 

} 

자바 스크립트 :

function signup(){ 
var p1 = _("pass1").value; 
var p2 = _("pass2").value; 
if(p1 != p2){ 

    status.innerHTML = "Your password fields do not match."; 

    document.getElementById("pass1").style.borderColor="red"; 

    document.getElementById("pass1").style.color="red"; 

    document.getElementById("pass2").style.borderColor="red"; 

    document.getElementById("pass2").style.color="red"; 
} 
} 

당신은 여기 않습니다보고 내가 전에 언급처럼, 자바 스크립트 파일 내가 뭘 원하는지 사용자가 "가입"버튼을 클릭 한 후에 만 ​​가능합니다. 내가 원하는 것을 할 수 있는지 알려주십시오. 미리 감사드립니다!

https://api.jquery.com/blur/

+2

'blur'이벤트를 사용하십시오. – Barmar

답변

1

내가 jQuery를 사용하는 것이 좋습니다,하지만 일반 JS에서 것이다 .. 초점의 반대는 흐림입니다

+0

좋아, 그걸 시도해 볼께. 작동하지 않는다면 jQuery를 사용해 볼 수도 있습니다. –

+0

왜 jQuery를 자바 스크립트에서 이미 할 수있는 간단한 것으로 추천하겠습니까? – Shryme

+0

그는 아마 내가 내 사이트를 현대화하는 것을 도우려고하기 때문에. 그러나 걱정하지 마라. 그것이 매력처럼 작동했다. 도움에 감사드립니다. 내가 할 수있을 때 나는이 대답을 받아 들일 것이다. –

2

을 (참고로, http://www.codesrce.com/signup.php로 이동하시기 바랍니다) 그리고 당신이에 onblur 이벤트를 사용할 수있는 HTML

<input id="pass2" class="signup-textboxes" placeholder="Confirm your password..." type="password" onfocus="emptyElement('status')" onblur="signup()" maxlength="100"> 
+0

그는 분명히 jQuery를 사용하지 않습니다. – Barmar

+0

아참. 그럼 .. 나는 대답을 기다리고있다. –

관련 문제