首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

B/S 一些CheckBox控件 Button提交时判断CheckBox选中状态checked属性解决方法

2012-06-02 
B/S 一些CheckBox控件 Button提交时判断CheckBox选中状态checked属性我用图片展示出了页面的效果我希望实

B/S 一些CheckBox控件 Button提交时判断CheckBox选中状态checked属性

我用图片展示出了页面的效果


我希望实现的功能是 判断页面上的 CheckBox控件上的属性 checked 

Button导出数据事件 时先判断至少1个CheckBox是选中状态 并提示

javascript或者jquery 或者 后台代码实现也行!

前台代码如下

C# code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SWDataMonitorExport.aspx.cs" Inherits="CTWebPlatform.DataMonitoring.SWDataMonitorExport" %><!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>     <link href="../Styles/Site.css" rel="stylesheet" type="text/css" /><script language="javascript" type="text/javascript">    function unselectall() {        if (document.myform.chkAll.checked) {            document.myform.chkAll.checked = document.myform.chkAll.checked & 0;        }    }    function CheckAll(form) {        for (var i = 0; i < form.elements.length; i++) {            var e = form.elements[i];            if (e.Name != 'chkAll' && e.disabled == false)            { e.checked = form.chkAll.checked; }        }    } </script></head><body>    <form id="form1" runat="server">    <div style="background-color:White;">       <br />         <table width="100%" class="style-table"><tr>                 <td class="style1">                <div class="style-saearch">                 <div class="search2to">                 <table border="0" cellpadding="0" cellspacing="0" class="Table_Search">                             <tr>                                 <td>                                     <span class="MarginLeft10">                                                                                                               </span>                                     <asp:RadioButton ID="RadioExcel" runat="server" Text="Microsoft Excel 工作表"                                       GroupName="Export" Checked="True" />                                 </td>                                 <td >                                                                        <asp:RadioButton ID="RadioWord" runat="server" Text="Microsoft Word 文档"                                          GroupName="Export" />                                                                    </td>                                 <td>                                                                         <asp:RadioButton ID="RadioTXT" runat="server" Text="超文本网页 Html"                                          GroupName="Export" />                                                                     </td>                                 <td>在线浏览&nbsp;                                                                          </td>                                 <td>                                                                         &nbsp;</td>                                 <td >                                                                    <asp:Button ID="ButtonHTTP" runat="server" CssClass="Button02" Height="24px"                                          Text="将数据导出" Width="84px" onclick="ButtonHTTP_Click"  />                                                                      </td>                             </tr>                         </table>                     </div>                     <div class="MarginLeft10">                                              <span class="MarginLeft5">请选择需要导出的&quot;列&quot;名称</span></div>                </div>              <div class="marginab BorderGray">                          <table style="width:100%; height: 300px;">                   <tr>                       <td>                    <span class="MarginLeft20"></span><input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox'/>全选√              </td>                       <td>&nbsp;                           </td>                       <td>&nbsp;                           </td>                       <td>&nbsp;                           </td>                       <td>&nbsp;                           </td>                   </tr>                   <tr>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckScrewPumpWellName" runat="server" Text="螺杆泵名称" />                       </td>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckDTUID" runat="server" Text="测电仪编号" />                       </td>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckOrgName" runat="server" Text="所属井场" />                       </td>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckSampleTime" runat="server" Text="采集时间" />                       </td>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckColumn1" runat="server" Text="启停机" />                       </td>                   </tr>                                      <tr>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckdPF" runat="server" Text="功率因数" />                           </td>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckdFreq" runat="server" Text="频率(hz)" />                           </td>                       <td>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckdSpeed" runat="server" Text="转速(/Min)" />                           </td>                       <td><%--Column1--%>                           <span class="MarginLeft20"></span>                           <asp:CheckBox ID="CheckColumn2" runat="server" Text="是否停机" />                           </td>                       <td>&nbsp;                                                    </td>                   </tr>                                </tr>                          </table>                     </div>                        </td>            </tr>           </table>              <br />            </div>    </form></body></html> 



[解决办法]

JQUERY实现方法:
//Button1 提交按钮的ID
$("#Button1").bind("click", function () {
var cbxs = $("#div1 input:checked"); //获取ID为DIV1的元素下面所有选中的复选框 
//alert(cbxs.length); //提示选中了几个
if (cbxs.length > 0) { 
return true; } 
else { alert('请先选择!'); return false; }
});
[解决办法]
HTML code
<!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 id="Head1" runat="server">    <title>导出数据</title>    <link href="../Styles/Site.css" rel="stylesheet" type="text/css" />    <script language="javascript" type="text/javascript">        function unselectall() {            if (document.myform.chkAll.checked) {                document.myform.chkAll.checked = document.myform.chkAll.checked & 0;            }        }        function CheckAll(form) {            for (var i = 0; i < form.elements.length; i++) {                var e = form.elements[i];                if (e.Name != 'chkAll' && e.disabled == false)                { e.checked = form.chkAll.checked; }            }        }        function getCheckCount() {            var inputs = document.getElementById('tab').getElementsByTagName('input');            for (var i = 0; i < inputs.length; i++) {                if (inputs[i].type == 'checkbox' && inputs[i] != 'chkAll' && inputs[i].checked) {                    return true;                }            }            alert('最少选择一项有效数据');            return false;        }    </script></head><body>    <form id="form1" runat="server">    <div style="background-color: White;">        <br />        <table width="100%" class="style-table">            <tr>                <td class="style1">                    <div class="style-saearch">                        <div class="search2to">                            <table border="0" cellpadding="0" cellspacing="0" class="Table_Search">                                <tr>                                    <td>                                        <span class="MarginLeft10"></span>                                        <asp:RadioButton ID="RadioExcel" runat="server" Text="Microsoft Excel 工作表" GroupName="Export"                                            Checked="True" />                                    </td>                                    <td>                                        <asp:RadioButton ID="RadioWord" runat="server" Text="Microsoft Word 文档" GroupName="Export" />                                    </td>                                    <td>                                        <asp:RadioButton ID="RadioTXT" runat="server" Text="超文本网页 Html" GroupName="Export" />                                    </td>                                    <td>                                        在线浏览&nbsp;                                    </td>                                    <td>                                        &nbsp;                                    </td>                                    <td>                                        <asp:Button ID="ButtonHTTP" runat="server" OnClientClick="return getCheckCount()"                                            CssClass="Button02" Height="24px" Text="将数据导出" Width="84px" />                                    </td>                                </tr>                            </table>                        </div>                        <div class="MarginLeft10">                            <span class="MarginLeft5">请选择需要导出的&quot;列&quot;名称</span></div>                    </div>                    <div class="marginab BorderGray">                        <table id="tab" style="width: 100%; height: 300px;">                            <tr>                                <td>                                    <span class="MarginLeft20"></span>                                    <input name='chkAll' type='checkbox' id='chkAll' onclick='CheckAll(this.form)' value='checkbox' />全选√                                </td>                                <td>                                    &nbsp;                                </td>                                <td>                                    &nbsp;                                </td>                                <td>                                    &nbsp;                                </td>                                <td>                                    &nbsp;                                </td>                            </tr>                            <tr>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckScrewPumpWellName" runat="server" Text="螺杆泵名称" />                                </td>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckDTUID" runat="server" Text="测电仪编号" />                                </td>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckOrgName" runat="server" Text="所属井场" />                                </td>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckSampleTime" runat="server" Text="采集时间" />                                </td>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckColumn1" runat="server" Text="启停机" />                                </td>                            </tr>                            <tr>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckdPF" runat="server" Text="功率因数" />                                </td>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckdFreq" runat="server" Text="频率(hz)" />                                </td>                                <td>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckdSpeed" runat="server" Text="转速(/Min)" />                                </td>                                <td>                                    <%--Column1--%>                                    <span class="MarginLeft20"></span>                                    <asp:CheckBox ID="CheckColumn2" runat="server" Text="是否停机" />                                </td>                                <td>                                    &nbsp;                                </td>                            </tr>                        </table>            </tr>        </table>        <br />    </div>    </form></body></html>试试 

热点排行