来自内置JavaScript的php的Jquery响应


Jquery response from php with javascript built-in

我有一个Javascript函数,它通过jquery将用户名和密码发送到php文件,然后得到一个响应以显示文本,然后根据它是否成功,它会以绿色或红色显示它。(绿色表示成功,红色表示其他任何内容):

function get() {
            $("#message").hide();
            $.post("login-exec", {
            login: form.login.value,
            password: form.password.value
            },
                function(output) {
                    if($.trim(output) == "Login Successful"){
                        document.getElementById("message").style.color = 'green';
                        $("#message").html(output).fadeIn(2000);
                    }
                    else{
                        document.getElementById("message").style.color = 'red';
                        $("#message").html(output).fadeIn(2000);
                    }
                });
        }

想知道我是否可以在 php 文件上使用 style.color 执行此逻辑,然后只需发送已经具有专有绿色、红色和文本响应的输出。就像以某种方式在响应中包含javascript,然后将其解析出来以运行javascript代码以及消息文本。这可能吗?

您可以使用

css 更改样式颜色,如 http://jsfiddle.net/TLkMF/所示。

如果 PHP 在小提琴中返回变量 output 的 json,它应该做你想做的事。

如果您还想设置消息文本,则可以按照 http://jsfiddle.net/TLkMF/1/所示进行操作