javascript如果语句执行但互相阻塞?

时间:2015-08-27 23:07:18

标签: javascript jquery

如果正在执行语句但未显示结果,则结果文本会显示几分之一秒,并会被另一个结果快速删除

<script>
function validateEmail($email) {
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    return emailReg.test($email);
}

function myFunction() {
    var user = $('#user-name').val();
    var userpass = $('#user-password1').val();
    var userpass1 = $('#user-password2').val();
    var useremail = $('#user-email').val();
    var passMatch = false;
    var nameValid = false;
    var emailValid = false;
    //Check Username availability
    if ($.trim(user) != '') {
        $.post('includes/checkUserName.php', {
            checkUser: user
        }, function(checkData) {
            $('#regErrors').text(checkData);
        });
    }
    //Check Email availability
    if ($.trim(useremail) != '') {
        $.post('includes/checkEmail.php', {
            checkE: useremail
        }, function(checkEmail) {
            $('#regErrors').text(checkEmail);
        });
    }
    if ($.trim(userpass) != userpass1) {
        $('#regErrors').text("Passwords must match")
    }
    if ($.trim(user) == '' || $.trim(userpass) == '' || $.trim(userpass1) == '' || $.trim(useremail) == '') {
        $('#regErrors').text("All fields required")
    }
}
</script>

1 个答案:

答案 0 :(得分:3)

您正在使用.text()方法替换每个帖子上的内容,而是添加标记:

$('#regErrors').append($("<p />", {html: checkData}));
$('#regErrors').append($("<p />", {html: checkEmail}));
$('#regErrors').append($("<p />", {html: "Passwords must match"}));
$('#regErrors').append($("<p />", {html: "All fields required"}));