当前位置:   article > 正文

jQuery:通过name获取div下的input的六种方法_根据name获取input的值

根据name获取input的值

1.获取input值 的6种方法:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>测试页面</title>
  6. <script type="text/javascript" src="jquery.js"></script>
  7. </head>
  8. <body>
  9. <div id="gjdiv" class="gjc">
  10. <input type="hidden" id="title" name="gj" value="113"/>
  11. <input type="hidden" id="brief" name="gj" value="df"/>
  12. <input type="hidden" id="content" name="gj" value="sg"/>
  13. </div>
  14. <script>
  15. $(document).ready(function(){
  16. var param0 = $("input[name='gj']").val();
  17. console.log("param0:"+param0);
  18. var param1 = $(".gjc input[name='gj']").val();
  19. console.log("param1:"+param1);
  20. var param2 = $("#gjdiv input[name='gj']").val();
  21. console.log("param2:"+param2);
  22. var param3 = $("div input[name='gj']").val();
  23. console.log("param3:"+param3);
  24. var param4 = $("#gjdiv>input[name='gj']").val();
  25. console.log("param4:"+param4);
  26. var param5 = $("#gjdiv :input[name='gj']").val();
  27. console.log("param5:"+param5);
  28. console.log("stringify():"+JSON.stringify(param0));
  29. //console.log(JSON.parse(param));
  30. })
  31. </script>
  32. </body>
  33. </html>
结果:

获取的都是第一个input的值。


2.将input值序列化:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>测试页面</title>
  6. <script type="text/javascript" src="jquery.js"></script>
  7. </head>
  8. <body>
  9. <div id="gjdiv" class="gjc">
  10. <input type="hidden" id="title" name="gj" value="113"/>
  11. <input type="hidden" id="brief" name="gj" value="df"/>
  12. <input type="hidden" id="content" name="gj" value="sg"/>
  13. </div>
  14. <script>
  15. $(document).ready(function(){
  16. var param0 = $("input[name='gj']").serialize();
  17. console.log("param0:"+param0);
  18. var param1 = $(".gjc input[name='gj']").serialize();
  19. console.log("param1:"+param1);
  20. var param2 = $("#gjdiv input[name='gj']").serialize();
  21. console.log("param2:"+param2);
  22. var param3 = $("div input[name='gj']").serialize();
  23. console.log("param3:"+param3);
  24. var param4 = $("#gjdiv>input[name='gj']").serialize();
  25. console.log("param4:"+param4);
  26. var param5 = $("#gjdiv :input[name='gj']").serialize();
  27. console.log("param5:"+param5);
  28. console.log("stringify():"+JSON.stringify(param0));
  29. //console.log(JSON.parse(param));
  30. })
  31. </script>
  32. </body>
  33. </html>

结果:


参考:

http://blog.csdn.net/dazhi_100/article/details/29582883



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/113226
推荐阅读
相关标签
  

闽ICP备14008679号