电子邮件验证实时消息,电子邮件会随机显示并停留

时间:2019-01-13 23:31:27

标签: php jquery html5

我正在尝试在验证消息中显示在电子邮件输入字段中键入的电子邮件。输入字段还已经在我的数据库中检查了已注册的电子邮件,然后根据结果显示一条消息。我然后添加了此代码from here

结果是验证程序将返回其消息,但电子邮件的跨度为空白。如果键入密钥,则电子邮件将显示一秒钟然后消失(由于验证程序重新检查了数据库),但是随后会再次显示“确认将发送到”消息,但电子邮件跨度保持空白。

<script>
//echo typed in email
    $('#youremail').keydown(function() {
    $('#youremail-confirm').text($(this).val());
    });
//check if the current user is already taken
$('#youremail').keyup(function(){
    var user = $('#youremail').val();
    var userlength = document.getElementById("youremail").value.length;
    if(userlength >= 8 && userlength <= 60) {
        $('.checkemail').html("");
        $.ajax({
            type: "GET",
            url: "<?php bloginfo('template_url'); ?>/ajax/check-username.php",
            data: "user=" + user,
            success: function(data){
                $('.checkemail').html(data);
            }
        });
    };
});
</script>
    <div class="form-label">
        <label for="youremail">Email Address <i>*</i></label>
        <small class="checkemail"></small>
    </div>
    <div class="form-input">
        <input type="email" name="youremail" id="youremail" class="input longinput" value="<?php echo $youremail; ?>" />
    </div> <!-- email -->



<?php 
if (strlen($_GET['user']) > 3 && strlen($_GET['user']) <= 30) {
if (username_exists($_GET['user'])) { //sanitizing is done by WordPress
    echo '<span class="checkusererr">This email is already used. Please <label for="lger" style="font-weight:bold;cursor:pointer;">click here log in</label> or enter another one.</span>';
} else {
    echo '<span class="checkuserok">Confirmation with be sent to <span id="youremail-confirm" style="font-weight:bold;"></span></span>';
} 
} ?>

1 个答案:

答案 0 :(得分:0)

我认为您需要将事件绑定到容器,然后该事件会冒泡至#youremail。在这种情况下,您将使用.form-input div。这只是概念的一个示例:

$('.form-input').on('keydown', '#youremail', function ()
{
    $('#youremail-confirm').text($(this).val());
});