Submit.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var Submit = {
  2. collectData : function($range, json){
  3. var serialize = [];
  4. if(json == null) {
  5. json = {};
  6. }
  7. if ($range) {
  8. serialize.push($range.find("input[type=hidden]"));
  9. serialize.push($range.find("input[type=text]"));
  10. serialize.push($range.find("input[type=tel]"));
  11. serialize.push($range.find("input[type=number]"));
  12. serialize.push($range.find("input[type=email]"));
  13. serialize.push($range.find("input[type=password]"));
  14. serialize.push($range.find("input[type=radio]:checked"));
  15. serialize.push($range.find("input[type=checkbox]:checked"));
  16. serialize.push($range.find("select"));
  17. serialize.push($range.find("textarea"));
  18. } else {
  19. serialize.push($("input[type=hidden]"));
  20. serialize.push($("input[type=text]"));
  21. serialize.push($("input[type=tel]"));
  22. serialize.push($("input[type=number]"));
  23. serialize.push($("input[type=email]"));
  24. serialize.push($("input[type=password]"));
  25. serialize.push($("input[type=radio]:checked"));
  26. serialize.push($("input[type=checkbox]:checked"));
  27. serialize.push($("select"));
  28. serialize.push($("textarea"));
  29. }
  30. // 转化表单数据
  31. for (var i = 0; i < serialize.length; i++) {
  32. serialize[i].each(function() {
  33. var name = $(this).attr("name");
  34. var value = $(this).val();
  35. if($.trim(name)){
  36. var oldValue = json[name];
  37. if(oldValue) {
  38. json[name]=oldValue+","+$.trim(value);
  39. } else {
  40. json[name]=$.trim(value);
  41. }
  42. }
  43. });
  44. }
  45. return json;
  46. },
  47. paramSubmit : function(url, callback, dataType, data, type, async) {
  48. var returnData = null;
  49. var param = {};
  50. param.url = url;
  51. param.type = type ? type : "post";
  52. param.dataType = dataType ? dataType : "json";
  53. param.data = $.param(data); // key=value&key=value
  54. param.async = async==false ? false : true;
  55. param.error = function(xhr, str, err) {
  56. console.log("请求出错,状态码:"+str+", 错误信息:"+err);
  57. }
  58. param.success = function(data) {
  59. returnData = data;
  60. if (callback) {
  61. callback(data);
  62. }
  63. }
  64. $.ajax(param);
  65. return returnData;
  66. }
  67. };