用querystring中的值预填充窗体

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用querystring中的值预填充窗体相关的知识,希望对你有一定的参考价值。

Code matches querysting data to form feilds on the page based on the feild name.
I've added the example querystring to the body of the page.

Found at: http://codingforums.com/showthread.php?t=198419
Code by: Old Pedant
  1. <head>
  2. <script type="text/javascript">
  3. function populate(form)
  4. {
  5. if ( location.search == null || location.search.length < 1 ) return; // no querystring
  6.  
  7. var pairs = location.search.substring(1).split("&");
  8. for ( var p = 0; p < pairs.length; ++p )
  9. {
  10. var pair = pairs[p].split("=");
  11. var name = pair[0];
  12. var value = unescape( pair[1].replace(/+/g, " ") );
  13. var fld = form.elements[name];
  14. var ftype = null;
  15. var farray = false;
  16. var atype = Array;
  17.  
  18. if ( fld != null )
  19. {
  20. if ( fld.length != null && fld.length >= 1 && fld[0].type != null && fld[0].type != undefined )
  21. {
  22. ftype = fld[0].type;
  23. farray = true;
  24. } else {
  25. ftype = fld.type;
  26. }
  27. }
  28. switch ( ftype )
  29. {
  30. case "text": case "hidden": case "textarea":
  31. if ( farray ) fld = fld[0]; // only handle first-named for this type
  32. fld.value = value;
  33. break;
  34. case "select-one": case "select-multiple":
  35. if ( farray ) fld = fld[0]; // only handle first-named for this type
  36. for ( var o = 0; o < fld.options.length; ++o )
  37. {
  38. var opt = fld.options[o];
  39. var oval = opt.value;
  40. if ( oval == null || oval == "" ) oval = opt.text;
  41. if ( oval == value )
  42. {
  43. opt.selected = true;
  44. break;
  45. }
  46. }
  47. break;
  48. case "checkbox": case "radio":
  49. if ( ! farray )
  50. {
  51. // single checbox or radio of that name:
  52. fld.checked = true;
  53. } else {
  54. for ( var cr = 0; cr < fld.length; ++cr )
  55. {
  56. if ( fld[cr].value == value )
  57. {
  58. fld[cr].checked = true;
  59. break;
  60. }
  61. }
  62. }
  63. break;
  64. default:
  65. alert("Unknown field type encountered for field " + name + ": " + ftype);
  66. break;
  67. } // end of switch
  68. } // end of loop on fields from qs
  69. }
  70. </script>
  71. </head>
  72. <body onload="populate(document.TheForm);">
  73. <p>
  74. Example querystring to try:<br/> ?sel1=Three&sel2=2&sel2=4&cb1=2&cb1=3&rb1=C&text1=put+some+random+text+here&ta1=all+fun+and+games...
  75. </p>
  76. <form name="TheForm">
  77. Text1<input type="text" name="text1" /><br/><br/>
  78. Checkboxs: 1:<input type="checkbox" name="cb1" value="1" />
  79. 2: <input type="checkbox" name="cb1" value="2" />
  80. 3: <input type="checkbox" name="cb1" value="3" />
  81. <br/><br/>
  82. RB1<br/>
  83. A<input type="radio" name="rb1" value="A" /><br/>
  84. B<input type="radio" name="rb1" value="B" /><br/>
  85. C<input type="radio" name="rb1" value="C" /><br/>
  86. <br/><br/>
  87. <textarea name="ta1" rows=4 cols=40></textarea>
  88. <br/><br/>
  89. Sel1:
  90. <select name="sel1">
  91. <option>One</option>
  92. <option>Two</option>
  93. <option>Three</option>
  94. <option>Four</option>
  95. </select>
  96. <br/><br/>
  97. Sel2:
  98. <select name="sel2" multiple>
  99. <option value="1">One</option>
  100. <option value="2">Two</option>
  101. <option value="3">Three</option>
  102. <option value="4">Four</option>
  103. </select>
  104. <br/>
  105. </form>
  106. </body>
  107. </html>

以上是关于用querystring中的值预填充窗体的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 使用查询字符串中的值预填充表单

我想围绕窗体中的按钮控件画一个矩形,然后在矩形里填充图片或颜色,用API怎么做?高分

QueryString 中的填充值

一行代码提取url中querystring的某个key的值

如何使用 access 数据库中的值填充列表或数组

如何将 url 中的参数传递给背包创建表单