比如有几个input<input name="t1" type="text" />
<input name="t2" type="text" />
<input name="t3" type="text" />
<input name="t4" type="text" />
提交表单时检查只能输入其中一项.代码怎么写?新手学习,请大家指点!

解决方案 »

  1.   

    <!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>
      

  2.   


    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)"/>
      

  3.   


    <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" />
      

  4.   

    var arr = [];
    $('input').eval(function(i, dom) {
        if($(this).val() !='') arr.push($(this));
    });
    if(arr.length > 1) return false;
      

  5.   

    var arr = [];
    $('input').each(function(i, dom) {
        if($(this).val() !='') arr.push($(this));
    });
    if(arr.length > 1) return false;