简体   繁体   中英

Having Issues with using VISIBLE. My Javascript doesnt seem to be handling the event when the check box is clicked

All of my hidden fields have ID's. And in my JavaScript in trying to set the visible from false to true on the payment selection. The Cash payment should just display the address to be sent to. The credit card payment comes up with text boxes and labels in order to process the payment online. But when I run the script the wont appear with the checkbox selection. I assigned an onClick event and still doesnt work. Any suggestions?

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ShoppingCart.aspx.cs" Inherits="ShoppingCart" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">

<head>
    <script type="text/javascript">
        function paymentFunction() {
            if (document.getElementById("rbCash").checked) {
                document.getElementById("lbCash").visible = true;
                document.getElementById("lbCash2").visible = true;
                document.getElementById("lbCash3").visible = true;
                document.getElementById("lbCash4").visible = true;
            } 
              else 
            {
            if(document.getElementById("rbCreditCard").checked)
            {
                document.getElementById("lbCard").visible = true;
                document.getElementById("lbCardNum").visible = true;
                document.getElementById("lbCVV").visible = true;
                document.getElementById("lbexp").visible = true;
                document.getElementById("ddlCard").visible = true;
                document.getElementById("tbCnum").visible = true;
                document.getElementById("tbcvvnum").visible = true;
                document.getElementById("tbexp").visible = true;


            }
          }
        }
    </script>

      <style type="text/css">
            .style1
            {
                width: 100%;
            }
          .style2
          {
              width: 100%;
          }
          .style3
          {
              width: 130px;
          }
        </style>

</head>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <table class="style2">
        <tr>
            <td class="style3">
                Payment Type:</td>
            <td>
                <asp:CheckBox ID="rbCash" runat="server" onClick="paymentFunction()" Text="Cash" ClientIDMode="Static"/>
                <asp:CheckBox ID="rbCreditCard" runat="server" onClick="paymentFunction()" Text="Credit Card" />
            </td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td>
                <asp:Label ID="lbCash" runat="server" ForeColor="Black" 
                    Text="Please Send Payment To:" Visible="False" ClientIDMode="Static"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td>
                <asp:Label ID="lbCash2" runat="server" ForeColor="Black" 
                    Text="Wild Style Shoes" Visible="False" ClientIDMode="Static"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td>
                <asp:Label ID="lbCash3" runat="server" ForeColor="Black" 
                    Text="1808 West Avenue" Visible="False" ClientIDMode="Static"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td>
                <asp:Label ID="lbCash4" runat="server" ForeColor="Black" 
                    Text="Chicago, IL 88947" Visible="False" ClientIDMode="Static"></asp:Label>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="lbcard" runat="server" Text="Card Type" Visible="False"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="ddlCard" runat="server" Visible="False">
                    <asp:ListItem>Select A Card</asp:ListItem>
                    <asp:ListItem>Visa</asp:ListItem>
                    <asp:ListItem>Discover</asp:ListItem>
                    <asp:ListItem>MasterCard</asp:ListItem>
                    <asp:ListItem>American Express</asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="lbcardnum" runat="server" Text="Card Number:" Visible="False"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="tbCnum" runat="server" Visible="False" Width="200px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="lbCVV" runat="server" Text="CVV Number" Visible="False"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="tbcvvnum" runat="server" Visible="False" Width="58px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                <asp:Label ID="lbexp" runat="server" Text="Expiration Date" Visible="False"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="tbexp" runat="server" Visible="False"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style3">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit Payment" Visible="False" />
            </td>
        </tr>
    </table>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        Thank You For Shopping With Us:<br />
    </p>
    <p>
    </p>
    <p>
    </p>
    <p>
    </p>
    <p>
    </p>


</asp:Content>

If you set Visible = false on the server side like you are doing, then the server will not render the control.

You probably want to do the following:

<asp:Label ID="lbCash" runat="server" ForeColor="Black" 
                    Text="Please Send Payment To:" style="visibility:hidden" ClientIDMode="Static"></asp:Label>

Then in the javascript do something like this:

document.getElementById('lbCash').style.visibility = 'visible';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM