日期:2014-05-18  浏览次数:20530 次

js判断textbox不为空
需求:我有10个TextBox,想通过js一次性判断这10个TextBox(10个textbox在一个div里)是否为空,如果全为空,alert错误信息;只要有一个不为空就继续别得操作。
这个怎么用JS实现?在线等

------解决方案--------------------
HTML code


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm6.aspx.cs" Inherits="CheckBox.WebForm6" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="text">
        <asp:TextBox ID="TextBox1" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox4" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox5" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox6" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox7" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox8" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox9" runat="server" Style="position: relative"></asp:TextBox>
        <asp:TextBox ID="TextBox10" runat="server" Style="position: relative"></asp:TextBox>
        
       
    </div>
        
    <input type="button" value="check" onclick="return check()" />
        
    <script type="text/javascript" >
    function checkNull()
    {
        var status=true;
        var inputs =document.getElementById("text").getElementsByTagName("input");
        for(var i=0;i<inputs.length;i++)
        {
            if(inputs[i].value!="" )
            {
                status=false;
            }
        }
        return status;
    }

    function check()
    {
        
        if(checkNull())
        {
            alert("不能全部為空");
            return false;
        }
    } 
    function trim(strText)
    {
        return strText.replace(/^\s+|\s+$/g, '');
    }
    </script>
    </form>
    

</body>
</html>