Radiobutton IF条件未在asp.net中的Javascript中执行

时间:2015-01-18 05:51:46

标签: javascript asp.net radio-button radiobuttonlist

这是我的代码:

asp.net:

            <table>
                <tr>
                    <td class="td1" colspan="2" style="width:100%;text-align:center;border-top-left-radius:10px;border-top-right-radius:10px">Make Payment</td>
                </tr>
                <tr>
                    <td class="td1" title="Payment Mode">Payment Mode:</td>
                    <td class="td2">
                        <asp:RadioButtonList ID="rblpaymentmode" runat="server" CssClass="lbidname" RepeatDirection="Horizontal" ToolTip="Select Payment Mode" OnSelectedIndexChanged="rblpaymentmode_SelectedIndexChanged" AutoPostBack="True" CausesValidation="True">
                            <asp:ListItem>Cheque</asp:ListItem>
                            <asp:ListItem>Cash</asp:ListItem>
                        </asp:RadioButtonList>
                    </td>
                </tr>
                <tr>
                    <td class="td1" title="Member Name">Member:</td>
                    <td class="td2">
                        <asp:DropDownList ID="ddlmember" runat="server" CssClass="ddl" ToolTip="Select Member">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td class="td1" title="Particular Type">Particular Type:<br />
                        <br />
                        <asp:Label ID="lbenterdetails" runat="server" Text="Enter Details:" ToolTip="Enter Details:" Visible="False"></asp:Label>
                    </td>
                    <td class="td2">
                        <asp:RadioButtonList ID="rblparticulartype" runat="server" CssClass="lbidname" RepeatDirection="Horizontal" OnSelectedIndexChanged="rblparticulartype_SelectedIndexChanged" ToolTip="Select Particular Type" AutoPostBack="True" CausesValidation="True">
                            <asp:ListItem Selected="True">Yearly Lawaazam</asp:ListItem>
                            <asp:ListItem>Other</asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:TextBox ID="txtotherparticulartype" runat="server" CssClass="textbox" ToolTip="Enter Details" Width="90%" Visible="False"></asp:TextBox>
                    </td>
                </tr>                    
                <tr>
                    <td class="td1" title="Cheque Date">Cheque Date:</td>
                    <td class="td2">
                        <asp:TextBox ID="txtchequedate" runat="server" CssClass="textbox" Width="90%" ToolTip="Select Cheque Date"></asp:TextBox>
                        <asp:CalendarExtender runat="server" ID="cetxtchequedate" CssClass="cal_Theme1" PopupPosition="Right" TargetControlID="txtchequedate" Format="yyyy-MM-dd"></asp:CalendarExtender>
                    </td>
                </tr>
                <tr>
                    <td class="td1" title="Cheque No.">Cheque No.:</td>
                    <td class="td2">
                        <asp:TextBox ID="txtchequeno" runat="server" CssClass="textbox" Width="90%" ToolTip="Enter Cheque No."></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="td1" title="Bank Name">Bank Name:</td>
                    <td class="td2">
                        <asp:TextBox ID="txtbankname" runat="server" CssClass="textbox" Width="90%" ToolTip="Enter Bank Name"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="td1" title="Branch Name">Branch Name:</td>
                    <td class="td2">
                        <asp:TextBox ID="txtbranchname" runat="server" CssClass="textbox" Width="90%" ToolTip="Enter Branch Name"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="td1">
                        <asp:Label ID="lbpaymentdate" runat="server"></asp:Label>
                    </td>
                    <td class="td2">
                        <asp:TextBox ID="txtpaymentdate" runat="server" CssClass="textbox" Width="90%" ToolTip="Enter Clearance Date"></asp:TextBox>
                        <asp:CalendarExtender runat="server" ID="cetxtclearancedate" CssClass="cal_Theme1" PopupPosition="Right" TargetControlID="txtpaymentdate" Format="yyyy-MM-dd"></asp:CalendarExtender>
                    </td>
                </tr>
                <tr>
                    <td class="td1" title="Amount">Amount:</td>
                    <td class="td2">
                        <asp:TextBox ID="txtamount" runat="server" CssClass="textbox" Width="90%" ToolTip="Enter Amount"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="td2" colspan="2" style="width:100%;text-align:center;background-color:#FFF6C1">
                        <asp:Panel ID="pnlchequeimage" runat="server" Visible="false">
                            <p style="padding-top:5px">Upload a cheque image to <a href="http://www.tinypic.com" target="_blank">TinyPic.com</a> &amp; paste the image url in the textbox below.</p> 
                            <asp:TextBox ID="txtchequeimage" CssClass="textbox" Width="60%" runat="server"></asp:TextBox>
                            <br />
                            <br />
                        </asp:Panel>
                    </td>                        
                </tr>
                <tr>
                    <td class="td1" colspan="2" style="width:100%;text-align:center;padding-bottom:10px;padding-top:10px;border-bottom-left-radius:10px;border-bottom-right-radius:10px">
                        <asp:Button ID="btnmakepayment" runat="server" CssClass="button" Text="Make Payment" OnClientClick="return validate()" ToolTip="Make Payment" Width="120px" />
                    </td>                        
                </tr>
            </table>

使用Javascript:

<script type="text/javascript">    
    function validate() {
 if (document.getElementById("<%=rblparticulartype.ClientID%>").value == "Other") {
            if (document.getElementById("<%=txtotherparticulartype.ClientID%>").value=="") {
                alert("Please enter details");
                document.getElementById("<%=txtotherparticulartype.ClientID%>").focus();
                return false;
         }              
    }
}
</script>
<br/>

我想检查,如果选择了radiobutton“Other”,则提交时文本框“txtotherparticulartype”不为空

但如果条件

,它不会进入Radiobutton的循环

请帮助!!!

1 个答案:

答案 0 :(得分:0)

一些更正和建议:

首先,您将visible设为false,txtotherparticulartype将其更改为true

其次,您有一个radionbutton列表,因此它的孩子会id这样:

rblparticulartype_0指的是第一个列表项的ID;

rblparticulartype_1指的是第二个的id 所以你可以尝试这个来获得检查的价值(不是我自己测试):

var value = document.getElemenById("rblparticulartype_0");

另一种方法是,您可以使用ListItem访问每个getElementsByName单选按钮,并使用以下索引:

var check = document.getElementsByName('rblparticulartype')[1].checked;

BTW你的代码应该是这样的:

<script type="text/javascript">
    function validate() { 
        var check = document.getElementsByName('rblparticulartype')[1].checked;
            if (check == true) {
                var value = document.getElementById("<%=txtotherparticulartype.ClientID %>").value;
                if (value == "") {
                    alert("Please enter details");
                    document.getElementById("<%=txtotherparticulartype.ClientID%>").focus();
                    return false;
                }
            }
        } 
</script>

或最后应该是这样的:

 <script type="text/javascript">
    function validate() {
        var radioButtons = document.getElementsByName("rblparticulartype"); 
        for (var x = 0; x < radioButtons.length; x++) {
            if (radioButtons[x].checked && radioButtons[x].value == "Other") {
                var value = document.getElementById("<%=txtotherparticulartype.ClientID %>").value;
                if (value == "") {
                    alert("Please enter details");
                    document.getElementById("<%=txtotherparticulartype.ClientID%>").focus();
                    return false;
                }
            }
        }
    }
</script>

我已经测试了他们完美运作的上述两种代码。

希望会有所帮助。