jquery插件-给所有匹配元素集合赋值
liulanggood
2012-08-22
1./**
2. * 给所有匹配元素集合赋值,跟据元素的propertyName从entity中取值 3. * @param {} entity 传递过来的java实体Bean对应的js对象 4. * @param {} [attrName] 可选参数,元素的属性名称(例如id,name等),默认为name属性;该属性值要和entity中的变量名对应 5. */ 6.$.fn.populate = function(entity, attrName) { 7. if(this.length > 0) { 8. attrName = attrName || "name"; 9. this.each(function(i){ 10. var tObj = $(this); 11. var propertyT = tObj.attr(attrName); 12. if(propertyT) propertyT = propertyT.replace(/[-+]?\d+/g, ""); 13. if(entity[propertyT]) { 14. var pValue = entity[propertyT]; 15. if("radio" == tObj.attr("type") || "checkbox" == tObj.attr("type")) { 16. if(pValue == tObj.val()) { 17. tObj.attr("checked",true); 18. } 19. }else { 20. tObj.val(pValue); 21. } 22. } 23. }); 24. } 25.}; |