当前位置:Gxlcms > JavaScript > 怎样做到浏览器跳转并传递参数

怎样做到浏览器跳转并传递参数

时间:2021-07-01 10:21:17 帮助过:21人阅读

这次给大家带来怎样做到浏览器跳转并传递参数,做到浏览器跳转并传递参数的注意事项有哪些,下面就是实战案例,一起来看一下。

one.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.slim.js"></script>
  7. </head>
  8. <body>
  9. <input type="text" class="keyword"/>
  10. <button id="searchBtn">点击</button>
  11. <script type="text/javascript">
  12. $("#searchBtn").click(function() {
  13. var searchText = jQuery.trim($(".keyword").val());
  14. var searchUrl = encodeURI("two.html?searchText=" + searchText); //使用encodeURI编码
  15. location.href = searchUrl;
  16. })
  17. </script>
  18. </body>
  19. </html>

two.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script src="https://cdn.bootcss.com/jquery/2.2.2/jquery.slim.js"></script>
  7. </head>
  8. <body>
  9. <input type="text" class="keyword1"/>
  10. <script type="text/javascript">
  11. //获取 上一个搜索页面传来的参数
  12. var searchUrl = window.location.href;
  13. var searchData = searchUrl.split("="); //截取 url中的“=”,获得“=”后面的参数
  14. var searchText = decodeURI(searchData[1]); //decodeURI解码
  15. $(".keyword1").val(searchText);
  16. </script>
  17. </body>
  18. </html>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

使用JS接收List<String>集合参数

函数节流与分时函数使用步骤详解

以上就是怎样做到浏览器跳转并传递参数的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行