我现在的写法是,比如表单有10个文本框
js:if(text1 == '') {
alert("text1不能为空");
return;
}
if(text2 == '') {
alert("text2不能为空");
return;
}
if(text1 == '')
.............现在是这样写的,有没有更好的做法
js:if(text1 == '') {
alert("text1不能为空");
return;
}
if(text2 == '') {
alert("text2不能为空");
return;
}
if(text1 == '')
.............现在是这样写的,有没有更好的做法
for (var i = 1; i <= 10; i++) {
var name = "text" + i;
var element = document.getElementById(name) || document.getElementsByName(name)[0];
if (!element) continue;
if (!element.value) {
alert(name + "不能为空");
return;
}
}/* TODO : 提交 */
for (var i = 1; i <= 10; i++) {
var name = document.getElementsByName("XXXXXXX");
var element = /^\w+$/;
if (!element.test(name[i].value)) {
alert(name[i]+ "不能为空");
return;
}
}
var name = document.getElementsByName("XXXXXXX");
for (var i = 1; i <= name.length; i++) {
if (!element.test(name[i].value)) {
alert(name[i]+ "不能为空");
return;
}
}
<head>
<title>无标题页</title>
<script type="text/javascript">
function Judge(){
var item = document.body.getElementsByTagName("input");
for(var i=0;i<item.length;i++){
if(item[i].type=="text"){
if(item[i].value==""){
alert(item[i].getAttribute("title")+"不可以为空!");
}
}
}
}
</script>
</head>
<body>
<input id="File1" type="file" />
<input id="UserName" title="用户名" type="text" />
<input id="Password" title="密码" type="text" />
<input id="ID" title="ID" type="text" />
<input id="Age" title="年龄" type="text" />
<input id="Button1" type="button" value="button" onclick="Judge()" />
</body>
</html>
$(function() {
$("#btnSubmit").click(function() {
//遍历所有的txtbox
$("input[type='text']").each(function() {
if ($(this).val().length == 0) {
alert('不能为空');
} })
}) })
</script>