mui表單驗證的實現(xiàn)
官網(wǎng):提交時校驗三個字段均不能為空,若為空則提醒并終止業(yè)務(wù)邏輯運行,使用each()方法循環(huán)校驗,如下:
var check = true;//一開始的時候設(shè)為true
mui("#input_example input").each(function() {
//若當前input為空,則alert提醒
if(!this.value || this.value.trim() == "") {
var label = this.previousElementSibling;
mui.alert(label.innerText + "不允許為空");
check = false;
return false;
}
}); //校驗通過,繼續(xù)執(zhí)行業(yè)務(wù)邏輯
if(check){
mui.alert('驗證通過!');
//通過后執(zhí)行總的提交
confirmDriver.onclick = driverVue.confirmDri();
}
plusready、mui.init()
在app開發(fā)中,若要使用HTML5+擴展api,必須等plusready事件發(fā)生后才能正常使用,mui將該事件封裝成了mui.plusReady()方法,涉及到HTML5+的api,建議都寫在mui.plusReady方法中。
mui.plusReady(function(){})
個人認為:
1.每個用到mui的頁面都調(diào)用下mui.init。
2.如果需要使用大H5+對象,就寫到plusReady中,如plus對象。
3.mui.init應(yīng)該放在mui.plusReady(function(){})
二維碼
生成二維碼,項目中的關(guān)鍵代碼:
引入插件
<script type="text/javascript" src="../js/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="http://cdn.staticfile.org/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
使用
$('#qrcode').qrcode({
render: "table", //table方式
width: 200, //寬度
height:200, //高度
text: "www.baidu.com" //任意內(nèi)容
});
利用url傳參數(shù)
傳送方
var loadNum = JSON.stringify(_self.loadNum);
location.href = currentUrl.split('html')[0] + 'html/driver_info.html?loadNum=' + loadNum;
接受方
//獲取url中的參數(shù)
function GetRequest() {
var url = location.search; //獲取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
var Request = new Object();
Request = GetRequest();
console.log(Request);
console.log(JSON.parse(Request.loadNum));
var loadNum = JSON.parse(Request.loadNum);
onclick
- 問題:將函數(shù)放在$(document).ready(function(){})中,沒有找到
- 解決:原因:It's because that function isn't in a global context, which is where your onclick="" is looking for it.
1.保持onclick,將function移出來
$(document).ready(function() {
alert('ready');
});
function doIt() {
alert('did it');
}
2.保留onclick
$(document).ready(function() {
alert('ready');
$("input[name='Go']").click(function() {
alert('did it');
});
});
js日期格式化
//格式化時間
function formatDate(){
var newDate = new Date();
return newDate.getFullYear()+'-'+(newDate.getMonth()+1)+'-'+newDate.getDate();
}
var newDate = formatDate();
console.log(newDate);
document.getElementById('nowDate').innerHTML = newDate;
分割線
<hr />