.val()不返回输入文本

时间:2016-05-12 14:34:31

标签: javascript jquery jsp

我可以从$('#checkEmail').val().trim())获取输入文字,但不能从$('#email').val().trim())获取。任何反馈都表示赞赏!

我的jsp代码:

            <form:form name="register-form" id="register-form" method="post" commandName="registerForm" action="${registerActionUrl}">
                <div class="newEmail">
                    <h4><spring:theme code="TXT001"/></h4>
                    <input type="text"  name="email" id="email" value="${newUserRegisterForm.email}">
                     <div id="form_errors">
                        <spring:hasBindErrors name="registerForm">
                            <c:forEach var="error" items="${loginerror.allErrors}">
                                <c:if test="${error.field == 'email'}">
                                    <li class="alert negative"><spring:message
                                            code="${empty error.defaultMessage ? error.code : error.defaultMessage}" /></li>
                               </c:if>
                           </c:forEach>
                       </spring:hasBindErrors>
                    </div>
                </div>
                <div class="newCEmail">
                    <h4>Confirm Email</h4>
                    <input type="text" name="checkEmail" id="checkEmail" value="">
                    <div id="form_errors">
                        <spring:hasBindErrors name="registerForm">
                            <c:forEach var="error" items="${loginerror.allErrors}">
                                <c:if test="${error.field == 'checkEmail'}">
                                    <li class="alert negative"><spring:message
                                            code="${empty error.defaultMessage ? error.code : error.defaultMessage}" /></li>
                               </c:if>
                           </c:forEach>
                       </spring:hasBindErrors>
                    </div>
                </div>
            </form:form>

我的javascript:

$('#register-form').on('focusout', '#email, #checkEmail', function() {
    $(this).removeClass('error');
    $(this).parent().find('.error-txt').remove();
    window.alert("email is " + $('#email').val().trim());
    window.alert("checkemail is " + $('#checkEmail').val().trim());

    if (!validateEmail($(this).val())) {
        $(this).addClass('error');
        $(this).parent().append('<p class="error-txt">Please enter valid email</p>');
    } else if ($('#email').val().trim() != "" && $('#checkEmail').val().trim() != "" && $('#checkEmail').val().trim() != $('#email').val().trim()) {
        $(this).addClass('error');
        $(this).parent().append('<p class="error-txt">A different email has been entered in Confirm Email</p>');
    }
});

function validateEmail(email) {
    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
}

我可以从$('#checkEmail').val().trim())获取输入文字,但不能从$('#email').val().trim())获取。任何反馈都表示赞赏!

2 个答案:

答案 0 :(得分:0)

我猜您需要先在标记中添加标识为email的元素

  <div class="newCEmail">
                <h4>Confirm Email</h4>
                <input type="text" name="email" id="email" value="">
                <input type="text" name="checkEmail" id="checkEmail" value="">
                <div id="form_errors">
                    <spring:hasBindErrors name="registerForm">
                        <c:forEach var="error" items="${loginerror.allErrors}">
                            <c:if test="${error.field == 'checkEmail'}">
                                <li class="alert negative"><spring:message
                                        code="${empty error.defaultMessage ? error.code : error.defaultMessage}" /></li>
                           </c:if>
                       </c:forEach>
                   </spring:hasBindErrors>
                </div>
            </div>

现在你的js代码应该像你期望的那样工作。

答案 1 :(得分:0)

字符串&#34;电子邮件&#34;不知何故不能在这里使用$(&#39; #mailAddress&#39;)。val()。trim())工作正常。