#JS 裏面的中文編碼
var kerwordsCode = encodeURI( kerwords ); //進行轉碼 JS 端
String keyword = URLDecoder.decode( keyword ,"utf-8"); //Java後臺進行解碼
#JS input選擇器
$("input[name='keleyicom']") 選擇所有的name屬性等於'keleyicom'的input元素
$("input[name!='keleyicom']") 選擇所有的name屬性不等於'keleyicom'的input元素
#全局CSS替換 Jquery
$(".values").css("color","green");
//先刪除,後添加。或者先添加後刪除均可
$(".aaa").addClass("bbb").removeClass("aaa");
$(".aaa").removeClass("aaa").addClass("bbb");
/***** 格式化整個Form,轉換成JSON格式 *****/
function getJsonForm( formId ){
if( varIsNUll( formId ) ){
formId = "defaultFormId" ;
}
console.log($('#' + formId ).serializeJSON());
console.log(JSON.stringify($('#' + formId ).serializeJSON()));
return JSON.stringify($('#' + formId ).serializeJSON());
}
/***** 判斷對象是否爲空 *****/
function varIsNUll( reValue ){
if (typeof(reValue) == "undefined" || reValue== null || reValue.length == 0 ) {
return true ;
}else{
return false ;
}
}
格式化JSON字符串 to JSON
var data = '{"status":"0","id":"14","filePath":"http://zcsjw-com.oss-cn-qingdao.aliyuncs.com/photo/common/supplier/pic/140199734915.png"}' ;
var json = JSON.parse(data);
console.log( json.filePath);
JS只取Double的兩位小數
(sum / dayCount).toFixed(2)
限制只能保留兩位小數點
<input type="text" onkeyup="num(this)" size="10"/>元
<input type="text" onkeyup='this.value=this.value.replace(/\D/gi,"")'/>
function num(obj){
obj.value = obj.value.replace(/[^\d.]/g,""); //清除"數字"和"."以外的字符
obj.value = obj.value.replace(/^\./g,""); //驗證第一個字符是數字
obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一個, 清除多餘的
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能輸入兩個小數
}
限制只能輸入數字
<input type="text" onkeyup='this.value=this.value.replace(/\D/gi,"")'/>
//公共日誌組件
log = function (s) {
try {
console.log(s);
} catch (e) {}
}
去掉A鏈接下劃線樣式:
a{text-decoration:none}
以及不可點擊
<a title="編輯" href ="javascript:return false;" onclick="return false;" style="cursor: default;">
JSTL判斷是否是最後一個元素
<c:forEach items="${pointSection}" var="chl" varStatus="stat">
<c:if test="${!stat.last}" >
<div class="item" id="_MYJD_repair" style="border-bottom-style: dotted;border-bottom-width: 1px;border-bottom-color: olive">
<a href="pointProductController.html?flag=flagPProductsection${chl.pointseId}" class="zltda">${chl.section}</a>
</div>
</c:if>
<c:if test="${stat.last}">
<div class="item" id="_MYJD_repair">
<a href="pointProductController.html?flag=flagPProductsection${chl.pointseId}" class="zltda">${chl.section}</a>
</div>
</c:if>
</c:forEach>
判斷屬性否爲空
<c:if test="${empty var1}">
var1 is empty or null.
</c:if>
<c:if test="${not empty var1}">
var1 is NOT empty or null.
</c:if>
指定select ID,某個值選中
/****
* 指定select ID,某個值選中
* @param selectId
* @param checkValue
*/
function set_select_checked(selectId, checkValue){
var select = document.getElementById(selectId);
for (var i = 0; i < select.options.length; i++){
if (select.options[i].value == checkValue){
select.options[i].selected = true;
break;
}
}
}
console.info兼容IE
/****
* 打印日誌,但是需要兼容IE,否則IE又執行不了
* @param msg
*/
function consoleLog(msg){
if (window["console"]){//判斷是否是IE
console.log(msg);
}
}
var a = $("input[name='radio']:checked").val();
Jquery Ajax
$.ajax({
url:"http://www.microsoft.com", //請求的url地址
dataType:"json", //返回格式爲json
async:true,//請求是否異步,默認爲異步,這也是ajax重要特性
data:{"id":"value"}, //參數值
type:"GET", //請求方式
beforeSend:function(){
//請求前的處理
},
success:function(req){
//請求成功時處理
},
complete:function(){
//請求完成的處理
},
error:function(){
//請求出錯處理
}
});
jquery獲取多個input元素屬性值
var name = $(this).attr("name"); //獲取name值
var val = $(this).val(); //獲取value值
jquery屬性操作
//兩種方法設置disabled屬性
$('#areaSelect').attr("disabled",true);
$('#areaSelect').attr("disabled","disabled");
//三種方法移除disabled屬性
$('#areaSelect').attr("disabled",false);
$('#areaSelect').removeAttr("disabled");
$('#areaSelect').attr("disabled","");
js獲取自定義的option屬性
var url = $(sobj).find("option:selected").attr("type");
//獲取select的option中的自定義屬性url
JS對GET請求的URL中文編碼
<script type="text/javascript">
var uriStr = "http://www.baidu.com?name=張三&num=001 zs";
var uriec = encodeURI(uriStr);
document.write("編碼後的" + uriec);
var uridc = decodeURI(uriec);
document.write("解碼後的" + uridc);
</script>
編碼後的http://www.baidu.com?name=%E5%BC%A0%E4%B8%89&num=001%20zs
解碼後的http://www.baidu.com?name=張三&num=001 zs