87,910
社区成员
发帖
与我相关
我的任务
分享
<input name="t1" type="text" />
<input name="t2" type="text" />
<input name="t3" type="text" />
<input name="t4" type="text" />
var arr = [];
$('input').each(function(i, dom) {
if($(this).val() !='') arr.push($(this));
});
if(arr.length > 1) return false;
var arr = [];
$('input').eval(function(i, dom) {
if($(this).val() !='') arr.push($(this));
});
if(arr.length > 1) return false;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#abv").click(function(){
var i=0;
$("input[type='text']").each(function(){
if($(this).val()!=""){
i++;
}
});
//alert(i);
if(i==1){
alert("提交成功");
}else{
alert("提交失败");
return false;
}
});
})
</script>
<input type="button" id="abv" value="测试用按钮"/>
<input name="t1" type="text" />
<input name="t2" type="text" />
<input name="t3" type="text" />
<input name="t4" type="text" />
function doOnFocus(id) {
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs .length; i++)
inputs[i].disabled = true;
document.getElementById(id).disabled = false;
}
<input name="t1" type="text" onfocus="doOnFocus(this.id)"/>
<input name="t2" type="text" onfocus="doOnFocus(this.id)"/>
<input name="t3" type="text" onfocus="doOnFocus(this.id)"/>
<input name="t4" type="text" onfocus="doOnFocus(this.id)"/>
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>
<script type="text/javascript">
function checkform(){
var j=0
for (i=0;i<4;i++){
if (document.forms[0].children[i].value!=""){j=j+1}
}
if (j==0){alert('一个都没有填');return false}
if (j>=2){alert('最多只能填一个');return false}
}
</script>
</head>
<body>
<form name="form1" method="post" action="?a=1" onsubmit="return checkform()">
<input name="t1" type="text" />
<input name="t2" type="text" />
<input name="t3" type="text" />
<input name="t4" type="text" />
<input type="submit" name="button" id="button" value="提交">
</form>
</body>
</html>