<script type="text/javascript">
mui.init();
document.addEventListener('DOMContentLoaded', function() {
document.getElementById("input_check").addEventListener('click', function() {
var check = true;
mui(".mui-input-group").each(function() {
//若当前input为空,则alert提醒
if(!this.value || this.value.trim() == "") {
var label = this.previousElementSibling;
mui.alert(label.innerText + "不允许为空");
check = false;
return false;
}
}); //校验通过,继续执行业务逻辑
check && mui.alert('验证通过!')
})
})
</script>
2 个回复
贾斯丁比伯 (作者) - asp.net工程师
<body>
<div class="mui-input-group">
<div class="mui-input-row">
<label >用户名:</label>
<input type="text" class="mui-input-clear" placeholder="请输入用户名">
</div>
<div class="mui-input-row">
<label>密码:</label>
<input type="password" class="mui-input-clear mui-input-password" placeholder="请输入密码">
</div>
<div class="mui-button-row">
<button id="input_check" type="button" class="mui-btn mui-btn-primary" >确认</button>
<button type="button" class="mui-btn mui-btn-danger" >取消</button>
</div>
</div>
</body>
回梦無痕 - 暂停服务
mui(".mui-input-group input").each(function() {
少了一个input吧?
贾斯丁比伯 (作者)
不是,我的代码会报这个错 Uncaught TypeError: Cannot read property 'innerText' of null at index.html:18
2018-06-13 11:47
回梦無痕
回复 贾斯丁比伯:你这个代码有问题的。加上input才是遍历input,不然就是在遍历div
2018-06-13 12:15