Implement form Element value write-back Based on JQuery
Form. jsp:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GB2312" %>
Form write-back<Script type = "text/javascript" src = "jquery. form_util.js "> </script> <script type =" text/javascript ">$ (function () {var obj = {name:" zhangsi ", area:" 591 ", sex: "11", enjoy: "100011", note: "xxxxxxxxxxx"}; FormUtil. set (obj) ;}); </script>Name:
Region:--- Select ---FuzhouXiamenNingde
Gender:MaleFemale
Interest:AABBCCDDEEFF
Note:
Jquery. form_util.js:
/** Form Element writeback tool * wumingkun 2014-08-19 */var FormUtil = {// value writeback method set: function (obj) {var attr; for (attr in obj) {var element = $ ("#" + attr) [0]; var nodeName = element. nodeName; if (nodeName = "INPUT") {FormUtil. judgeType ($ (element ). attr ("type"), attr, obj [attr]);} else if (nodeName = "SELECT") {FormUtil. setSelect (attr, obj [attr]);} else if (nodeName = "TEXTAREA") {FormUtil. setValue (attr, obj [attr]) ;}}, // set the Text Field Password box in the text box to the hidden box setValue: function (key, value) {$ ("#" + key ). val (value) ;}, // set the setSelect: function (key, value) {$ ("#" + key + "option") in the drop-down box "). each (function () {if (this. value = value) {$ (this ). attr ("selected", "selected") ;}}) ;}, // set radiosetRadio: function (key, value) {$ (": radio [name = '"+ key +"'] "). attr ("checked", false); $ (": radio [value = '" + value + "']"). attr ("checked", true) ;}, // set checkboxsetCheckbox: function (key, value, isDefault) {$ (": checkbox [name = '"+ key +"'] "). attr ("checked", false); if (isDefault) {var vs = value. split (""); for (var I =. length-1; I> = 0; I --) {if (vs [I] = "1") {$ (": checkbox [name = '"+ key +"'] "). filter (": eq (" + (. length-1-i) + ")"). attr ("checked", true) ;}} else {}}, // judge judgeType: function (type, key, value) of the input type) {if (type = "text" | type = "password" | type = "hidden") {FormUtil. setValue (key, value);} else if (type = "radio") {FormUtil. setRadio (key, value);} else if (type = "checkbox") {FormUtil. setCheckbox (key, value, true );}}};