【问题标题】:How to md5 jQuery variable? [duplicate]如何 md5 jQuery 变量? [复制]
【发布时间】:2017-04-09 11:59:56
【问题描述】:

我需要md5 输入密码,然后才能通过邮寄方式发送密码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<input type="text" name="password" id="password"><input type="submit" name="submit" value="submit" onclick="encrpt()">

<script>
function encrpt(){
    var password = $("#password").val();
$("#password").val("<?php echo md5(1234); ?>");
    }
</script>

此代码工作正常,md5 值 12345 设置为密码字段,但每当我使用 jQuery 密码值时,它会给出错误的值,例如

$("#password").val("<?php echo md5('<script>password</script>'); ?>");

我需要md5 我在字段中输入的密码值。

【问题讨论】:

标签: php jquery


【解决方案1】:

您不能在 javascript 函数中使用 PHP md5。为此,您必须使用外部 JS 库。例如:https://github.com/blueimp/JavaScript-MD5

所以你的代码应该是这样的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.5.0/js/md5.min.js"></script>

<input type="text" name="password" id="password"><input type="submit" name="submit" value="submit" onclick="encrpt()">

<script>
    function encrpt() {
        var password = $("#password").val();
        $("#password").val(md5(password));
    }
</script>

【讨论】:

    【解决方案2】:

    查看 jQuery md5 库,因为使用纯 jQuery 无法做到这一点:https://github.com/placemarker/jQuery-MD5

    【讨论】:

      【解决方案3】:

      你可以使用CrypotJs来达到目的

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">      </script>
      <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
      
      <input type="text" name="password" id="password"><input type="submit" name="submit" value="submit" onclick="encrpt()">
      
        <script>
         function encrpt(){
         var password = $("#password").val();
         $("#password").val(CryptoJS.MD5(password).toString());
         }
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 1970-01-01
        • 2017-07-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多