刷新页面后无法隐藏和显示文本框

时间:2014-10-06 09:48:15

标签: c# jquery

我正在尝试使用jquery通过单选按钮隐藏和显示文本框。我可以显示和隐藏文本框但错误是我有一个下拉列表。当我选择下拉值时,页面会刷新。页面刷新后,我无法隐藏和显示文本框。我有ajax的更新面板。为什么我在页面刷新后无法隐藏和显示文本框?这是我的源代码。请帮帮我。

<%@ Page Title="" Language="C#" MasterPageFile="~/Home.Master" AutoEventWireup="true" CodeBehind="ExperienceADD.aspx.cs" Inherits="Manjilas.WebForm31"%>
<%@ Register Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" tagPrefix="ajax" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
<head>
 <script src="Scripts2/jquery-1.7-vsdoc.js"></script>
 <script src="Scripts2/jquery-1.7.js"></script>
 <script src="Scripts2/jquery-1.7.min.js"></script>
 <script type="text/javascript">
        $(function () {
        $('input[name="type"]').on('click', function () {
        if ($(this).val() == 'Experienced') {
            $('#txtcomp').Show();
            $('#txtfrom').Show();
            $('#txtto').Show();

       } else {
       $('#txtcomp').hide();
            $('#txtcomp').hide();
            $('#txtfrom').hide();
            $('#txtto').hide();

        }
    });
   });
  </script>
  </head>    
  <div class="container-fluid">
<div class="row-fluid">
            <div class="well span5 center login-box">
                <div class="alert alert-info">
                <b><font size="4">ADD EXPERIENCE DETAILS</font></b>
                </div>
                <form id="form1" runat="server">

                <asp:UpdatePanel ID="updatepanel1" runat="server"><ContentTemplate>
                <div>
                    <ajaxToolkit:ToolkitScriptManager runat="server">
                    </ajaxToolkit:ToolkitScriptManager>

                  <asp:UpdatePanel ID="updatepanel2" runat="server"></asp:UpdatePanel>


                    <fieldset>

                        <table class="ui-accordion">
                        <tr>
                                <td align="left" class="style2">
                                    MachID</td>
                                <td align="left">
                              <%--   <div class="input-prepend" title="Select Country   Name">--%>
                                    <asp:DropDownList ID="ddid" runat="server" AutoPostBack="True" 
                                         onselectedindexchanged="ddid_SelectedIndexChanged"> 

                                    </asp:DropDownList>
                                </td>

                            </tr>
                              <tr>
                                <td align="left" class="style2">
                                    <asp:Label ID="Empcode" runat="server" Text="EmpCode"></asp:Label>
                                <td align="left">


                                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                                 </td>

                            </tr>

                                <tr>
                                <td align="left" class="style2">
                                </td>
                                <td align="left">
                                 <input type="radio" name="type" value="Fresher" />Fresher
                                 <input type="radio" name="type" value="Experienced" />Experienced
                                </td>
                            </tr>
                             <tr>
                                <td align="left" class="style2">
                                  Company</td>
                                <td align="left">
                                 <div class="input-prepend" title="Autogenerated District ID" data-rel="tooltip">
                                    <asp:TextBox ID="txtcomp" runat="server" TextMode="SingleLine"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="left" class="style2">
                                  From Date</td>
                                <td align="left">
                                 <div class="input-prepend" title="Enter District Name" data-rel="tooltip">
                                    <asp:TextBox ID="txtfrom" runat="server" TextMode="SingleLine"></asp:TextBox>
                                     <ajax:CalendarExtender ID="Calendarextender1" TargetControlID ="txtfrom" Format="dd/MM/yyyy" runat="server"></ajax:CalendarExtender> 

                                </td>
                            </tr>
                            <tr>
                                <td align="left" class="style2">
                                  To Date</td>
                                <td align="left">
                                 <div class="input-prepend" title="Enter District Name" data-rel="tooltip">
                                    <asp:TextBox ID="txtto" runat="server" TextMode="SingleLine"></asp:TextBox>
                                     <ajax:CalendarExtender ID="Calendarextender2" TargetControlID ="txtto" Format="dd/MM/yyyy" runat="server"></ajax:CalendarExtender> 

                                </td>
                            </tr>
      <tr>
                                <td class="style2">
                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    &nbsp; </td>
                                <td align="left">
                                  &nbsp;<asp:Button ID="Button1" class="btn-primary" runat="server" Text="Add" 
                                    Height="36px" Width="74px" onclick="Button1_Click" />
                                    &nbsp;&nbsp;&nbsp;
                                    <asp:Button ID="Button2" class="btn-primary" runat="server" Text="Cancel" 
                                    Height="36px" Width="74px" PostBackUrl="~/districtDetails.aspx" />
                                </td>
                            </tr>
                             <tr>
                                 <td class="style2">
                                     &nbsp;</td>
                                 <td align="left">
                                     <asp:Label ID="Label2" runat="server" ForeColor="Red"></asp:Label>
                                 </td>
                             </tr>
                        </table>
                        </div>
                        </ContentTemplate>
                        </asp:UpdatePanel>


                        </fieldset>
                </form>

            </div><!--/span-->
        </div><!--/row-->
        </div>
  </div>

  </asp:Content>

2 个答案:

答案 0 :(得分:0)

show函数是k感性的。试试这个:

$('#txtcomp,#txtfrom,#txtto').show();

答案 1 :(得分:0)

这是因为您正在使用UpdatePanel,它会刷新其中<ContentTemplate>内的所有内容,在您的情况下input[name="type"]内有on('click', function() {

由于它已刷新,radiobutton不再将当前name="type"click绑定,因此dropdown之后永远不会触发UpdatePanel你在<asp:UpdatePanel ID="updatepanel1" runat="server"> <ContentTemplate> <tr> <td align="left" class="style2"> MachID </td> <td align="left"> <asp:DropDownList ID="ddid" runat="server" AutoPostBack="True" onselectedindexchanged="ddid_SelectedIndexChanged"> </asp:DropDownList> </td> </tr> <tr> <td align="left" class="style2"> <asp:Label ID="Empcode" runat="server" Text="EmpCode"></asp:Label> </td> <td align="left"> <asp:TextBox ID="txtname" runat="server"></asp:TextBox> </td> </tr> </ContentTemplate> </asp:UpdatePanel> 中选择了一个项目,你需要的是仅为你想要刷新或改变的东西包裹jQuery on click selector,就像你的情况一样:

$('input[name="type"]').on('click', function () {
    if ($(this).val() == 'Experienced') {
        $('#txtcomp').show();
        $('#txtfrom').show();
        $('#txtto').show();
}

其他方式

使用$('.ui-accordion').on('click', 'input[name="type"]', function () { if ($(this).val() == 'Experienced') { $('#txtcomp').show(); $('#txtfrom').show(); $('#txtto').show(); } ,您只需要更改

{{1}}

{{1}}