JS开发最佳实践
JS
刷新页面
window.location.reload(true);
JQuery
元素定位
按class查询 : $(".myclass") 按ID查询 : $("#myid") 定位某个元素,查找子元素 : $(".quickMenu i").bind('click', function () { 函数里获得this的子元素 : $(this).find('i').toggleClass("ticked");
Ajax 调用
jQuery.each( [ "put", "delete" ], function( i, method ) { jQuery[ method ] = function( url, data, callback, type ) { if ( jQuery.isFunction( data ) ) { type = type || callback; callback = data; data = undefined; } return jQuery.ajax({ url: url, type: method, dataType: type, data: data, success: callback }); }; }); $.put('http://stackoverflow.com/posts/22786755/edit', {text:'new text'}, function(result){ console.log(result); })
对应的spring mvc 代码
@RequestMapping(value = "resubmit/{id}", method = RequestMethod.PUT) @ResponseStatus(HttpStatus.CREATED) @ResponseBody public String reSubmitVMApply(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws BindException {
$('#submitBtn').click(function () { //alert($('#reason').val()); if( $('#reason').val() == undefined || $('#reason').val() == null || $('#reason').val() == ""){ $("#alertNull").css("color","red"); return false; } return submitApply(); }); function submitApply() { var formParam = $("#vmApplyForm").serialize();//序列化表格内容为字符串 $.ajax({ type: 'post', url: '/air/vms', data: formParam, cache: false, // dataType: 'json', success: function (data) { //alert("data:"+data); $("#alertNull").css("color","#3a87ad"); $("#alertSuccess").css("display","block");//delete if(data.toString().equals("草稿")){ $("#alertFail").css("display","block"); }else{ $("#alertSuccess").css("display","block"); } } }); return false; }
对应java 代码
@RequestMapping(method = RequestMethod.POST, produces = "text/html;charset=UTF-8") @ResponseStatus(HttpStatus.CREATED) @ResponseBody public String createVMApply(@Valid VMApply vmApply, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws BindException {