常用js
原文链接:慧言博客
[toc]
1.表单提交为json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| function addBasic() { console.log($('#basic').serializeJson()) $.ajax({ type: "POST", dataType: "json", url: '../basicInfo', contentType: "application/json", data:JSON.stringify($('#basic').serializeJson()), success: function (result) { console.log("data is :" + result) if (result.code == 200) { alert("登陆成功"); window.location.href = "../home/home_page.html"; }else { alert(result.message) } } }); }
(function (window, $) { $.fn.serializeJson = function () { var serializeObj = {}; var array = this.serializeArray(); $(array).each( function () { if (serializeObj[this.name]) { if ($.isArray(serializeObj[this.name])) { serializeObj[this.name].push(this.value); } else { serializeObj[this.name] = [ serializeObj[this.name], this.value]; } } else { serializeObj[this.name] = this.value; } }); return serializeObj; }; })(window, jQuery);
|
2.上传图片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| $("#m_image_addr").live("change",function(){ var file=$("#m_image_addr")[0].files[0]; var formData = new FormData(); formData.append("m_image_addr",file); var index=file.name.lastIndexOf("."); var type=file.name.substring(index); if(type!=".jpg"&&type!=".png"){ alert("只能上传jpg和png格式的图片!!"); return; } $.ajax({ url:"../upload.do", data:formData, dataType:"text", type:"post", contentType: false, processData: false, success:function (path) { $("#yulan").attr("src",path); $("input[name='imgUrl']").attr("value",path); } }) })
|
3.加载前端数据到表单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| $.get("/basicInfo/"+$.cookie('id'),{},function(result){ if (result.resp_code=="0"){ loadData(result.datas); }else{ $("#msg2").text(result.resp_msg); } },"json"){ if (result.resp_code=="0"){ loadData(result.datas); }else{ $("#msg2").text(result.resp_msg); } },"json") function loadData(obj){ if($.type(obj)!="object"){ alert("页面初始化错误!"); return false; } var key,value,tagName,type,arr; for(x in obj){ key = x; value = obj[x]; $("[name='"+key+"'],[name='"+key+"[]']").each(function(index){ tagName = $(this)[0].tagName; type = $(this).attr('type'); if (tagName=='IMG'){ $(this).attr('src',value); } if(tagName=='INPUT'){ if(type=='radio'){ $(this).attr('checked',$(this).val()==value); }else if(type=='checkbox'){ for(var i =0;i<value.length;i++){ if($(this).val()==value[i]){ $(this).attr('checked',true); break; } } }else if(type=='date'){ if(parseInt(value)>1000000000000) $(this)[0].valueAsNumber=parseInt(value); else if(parseInt(value)>1000000000) $(this)[0].valueAsNumber=parseInt(value)*1000; else $(this)[0].valueAsDate=new Date(value); }else{ if($.isArray(value)) $(this).val(value[index]); else $(this).val(value); } }else if(tagName=='SELECT'){ $(this).val(value); } }); } }
|
4.单按钮开关
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| $("#daohang").click( function () { var isON=$(this).attr("isON"); if(isON!="1"){ $("#content ").css({'margin-left':'220px'}); $("#sidebar").children("ul").css({'display': 'block'}); $("#sidebar").show(); $("#zhankai").text("关闭导航"); isON=1; }else{ $("#content ").css({'margin-left':'0px'}); $("#sidebar").children("ul").css({'display': 'none'}); $("#sidebar").hide(); $("#zhankai").text("展开导航"); isON=0; } $(this).attr("isON",isON); } )
|
5.回车提交事件
$("#verify").bind("keydown",function(event){ if(event.keyCode == "13") { } })