有几个文本框,求只能输入其中之一个的JS代码

sir424 2013-05-09 04:15:20
比如有几个input

<input name="t1" type="text" />
<input name="t2" type="text" />
<input name="t3" type="text" />
<input name="t4" type="text" />


提交表单时检查只能输入其中一项.代码怎么写?新手学习,请大家指点!
...全文
145 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 3 楼 z284122755 的回复:

<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" />
++++++++++
  • 打赏
  • 举报
回复
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" />
rencht 2013-05-09
  • 打赏
  • 举报
回复

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)"/>
asdf147asdf 2013-05-09
  • 打赏
  • 举报
回复
<!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>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧