-2
<script language="JavaScript">
    function validate(x) {



        var cur_p = document.getElementById('current').value;
        var new_p = document.getElementById('new').value;
        var con_p = document.getElementById('confirm').value;

        document.getElementById('msg_p').innerHTML = '';
        document.getElementById('msg_cur').innerHTML = '';


        if(x != cur_p)
        {   document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
            return false;
        }   

        if(new_p != con_p)
        {   document.getElementById('msg_p').innerHTML = 'Passwords do not match';
            return false;
        }

        return (true);

    }
</script>

html

<form action='change-password.php' method='post' onsubmit="return validate('<?=$current?>')" >

我的代码上有这些。我无法同时显示这些 if 的结果。

if(x != cur_p)
and 
if(new_p != con_p)

如果我把

if(x != cur_p){} 

在顶部

if(new_p != con_p){}

if(x != cur_p) 的响应结果会显示,后者不会

反之亦然。

我如何显示这两个 if 的结果(假设满足那些条件)

4

2 回答 2

3

问题是你false在第一个之后返回,所以第二个永远不会到达。相反,在每个中设置一个布尔变量并返回布尔变量(true如果两者都没有失败,或者false其中一个失败)。

    // Boolean variable starts true, will get set to false if either condition is met:
    var okFlag = true;
    if(x != cur_p)
    {   document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
        // Set to false
        okFlag = false;
    }   

    if(new_p != con_p)
    {   document.getElementById('msg_p').innerHTML = 'Passwords do not match';
        // Set to false
        okFlag = false;
    }
    // Return the flag, which is either true or false.
    return okFlag;
于 2012-11-27T03:32:20.567 回答
2

首先,您的代码中有错字

document.getElementById('msg_p').innerHTM = '';  <-- Missing an L

其次,当然如果你返回,它会退出函数。所以代码不会执行这两个语句。

改变

    if(x != cur_p)
    {   document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
        return false;
    }   

    if(new_p != con_p)
    {   document.getElementById('msg_p').innerHTML = 'Passwords do not match';
        return false;
    }

    return (true);

    var isValid = true;
    if(x != cur_p)
    {   document.getElementById('msg_cur').innerHTML = ' Your password was incorrect';
        isValid = false;
    }   

    if(new_p != con_p)
    {   document.getElementById('msg_p').innerHTML = 'Passwords do not match';
        isValid = false;
    }

    return isValid;
于 2012-11-27T03:32:54.833 回答