2017.8.9mui验证、二维码、url传参、日期格式化

mui表单验证的实现

官网:提交时校验三个字段均不能为空,若为空则提醒并终止业务逻辑运行,使用each()方法循环校验,如下:

var check = true;//一开始的时候设为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;
}
}); //校验通过,继续执行业务逻辑 
if(check){
    mui.alert('验证通过!');
  //通过后执行总的提交
  confirmDriver.onclick = driverVue.confirmDri();
}

plusready、mui.init()

在app开发中,若要使用HTML5+扩展api,必须等plusready事件发生后才能正常使用,mui将该事件封装成了mui.plusReady()方法,涉及到HTML5+的api,建议都写在mui.plusReady方法中。

mui.plusReady(function(){})

个人认为:
1.每个用到mui的页面都调用下mui.init。
2.如果需要使用大H5+对象,就写到plusReady中,如plus对象。
3.mui.init应该放在mui.plusReady(function(){})

二维码

生成二维码,项目中的关键代码:
引入插件

<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" //任意内容 
});

利用url传参数

传送方

var loadNum = JSON.stringify(_self.loadNum);
location.href = currentUrl.split('html')[0] + 'html/driver_info.html?loadNum=' + loadNum;

接受方

//获取url中的参数
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

  • 问题:将函数放在$(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 />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,268评论 25 708
  • MUI背景介绍 MUI是一套前端框架,由DCLOUD公司研发而成,提供大量H5和js语言组成的组件,大大提高了开发...
    jackzhouyu阅读 29,832评论 2 76
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,523评论 0 17
  • 文/珂苒娘亲 写于/2017.10.19 图片来源/网络 故事来源/亮妈童诗 秋风吹起来,蒲公英宝...
    珂苒娘亲阅读 924评论 3 5
  • 工作快两年半了,之前一直在吸收大神们的博文精髓,是时候开始回馈社会,让越来越多人能够快速掌握工作技能。
    wingCI阅读 266评论 0 0