將json格式轉為url參數格式的方法(xjl456852整理修改)

來源:互聯網
上載者:User

標籤:href   encodeuri   return   func   www   code   parse   jquer   else   

測試頁面:
  1. <html>
  2. <head>
  3. <script type="text/javascript" src="jquery-1.11.3.min.js"></script>
  4. </head>
  5. <body>
  6. <div id="div1">
  7. <button onclick="test1()">
  8. t1
  9. </button>
  10. <button onclick="test2()">
  11. t2
  12. </button>
  13. </div>
  14. </body>
  15. <script type="text/javascript">
  16. var parseParam = function(param, key) {
  17. var paramStr = "";
  18. if (typeof param == ‘string‘ || typeof param == ‘number‘ || typeof param == ‘boolean‘) {
  19. paramStr += "&" + key + "=" + encodeURIComponent(param);
  20. } else {
  21. $.each(param, function(i,tmp) {
  22. var k = key == null ? i : key + (param instanceof Array ? "[" + i + "]" : "." + i);
  23. paramStr += ‘&‘ + parseParam(tmp, k);
  24. });
  25. }
  26. return paramStr.substr(1);
  27. };
  28. function test1() {
  29. console.log(parseParam({a:"12",b:123}));
  30. }
  31. function test2() {
  32. console.log(parseParam({a:"12",b:123},"user"));
  33. }
  34. </script>
  35. </html>

測試結果:a=12&b=123user.a=12&user.b=123

null

將json格式轉為url參數格式的方法(xjl456852整理修改)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.