how to clear server textbox using jquery

喜夏-厌秋 提交于 2019-12-13 08:02:02

问题


i am using a client function to clear a textbox (server control runat="server") so when i clear it using jquery it appears empty but when i trace the code and check the textbox.Text control i found the value there and not null so how to clear it also from value property of the textbox control from the client side(i have to clear it from client for user interaction)

i am using the following to clear it from client side code:

$("#cp1_txtDeathDate").val("");

this the code of my control :

<asp:TextBox ID="txtDeathDate" runat="server" ></asp:TextBox> 

in code behind :

if (txtDeathDate.Text != "" && DatePattern.IsMatch(txtDeathDate.Text))
{
//do something
}

at firebug trace:

<input id="cp1_txtDeathDate" type="text" value="26/10/2012" name="ctl00$cp1$txtDeathDate"> // while textbox appeared empty

and i am calling the javascript code when the user change value of checkbox by (event click)

        function checkDead_click() {


            if ($("#cp1_chDead").prop("checked") == false) {
                $("#cp1_drpDeathReason").attr('disabled', 'disabled');
                $("#cp1_txtDeathDate").attr('disabled', 'disabled');
                $('#divDeath input#cp1_radDMR_0').attr('checked', true);
                $("#divDeath input:radio").attr("disabled", true);
                $("#cp1_drpDeathReason").html("");
                $("#cp1_txtDeathDate").val("");
            }
            else {
                $("#cp1_drpDeathReason").removeAttr('disabled');
                $("#cp1_txtDeathDate").removeAttr('disabled');
                $("#divDeath input:radio").removeAttr('disabled');
            }

        }

$("#cp1_chDead").click(checkDead_click);


protected void Saveform()
    {
        Demographic Demo = new Demographic();


            using (DBEntities DB = new DBEntities())
            {
                try
                {
                    if (hdFormMode.Value == "edit")
                    {
                        string nid = Session["NID"].ToString();
                        Demo = DB.Demographics.SingleOrDefault<Demographic>(d => d.NID == nid);
                    }
                    if (Demo != null || hdFormMode.Value == "new")
                    {

                        Demo.NID = litNID.Text;
                        Demo.BirthDate= txtBirthDate.Text;
                        Demo.FirstName = txtFirstN.Text;
                        Demo.FatherName = txtFatherN.Text;
                        Demo.GrandFName = txtGrandFN.Text;
                        Demo.FamilyName = txtFamilyN.Text;

                        if (txtDeathDate.Text != "" && DatePattern.IsMatch(txtDeathDate.Text))
                        {

                            Demo.DeathDate = txtDeathDate.Text;
                            Demo.RealDeathDate = Convert.ToByte("1");
                         }


                        else
                        {
                            Demo.DeathDate = null;

                        }
                        if (chDead.Checked)
                            Demo.Dead = Convert.ToByte("1");
                        else
                        {
                            Demo.Dead = null;
                            Demo.DeathReason = null;
                            Demo.RealDeathDate = null;
                            Demo.DeathDate = null;

                        }

                        if (hdFormMode.Value == "new")
                        {
                            CreateDemo(Demo);

                        }
                        else
                        {
                            if (Demo.EntityState == EntityState.Detached)
                            {

                                DB.AttachTo("DBEntities.Dempographics", Demo);
                            }
                            DB.ObjectStateManager.ChangeObjectState(Demo, EntityState.Modified);
                            DB.SaveChanges();
                        }

                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }

    }

回答1:


You should be using this to get the client ID of your server control

var txtDeathDate = "<%= txtDeathDate.ClientID %>";

//in your actual code should be
$("<%= txtDeathDate.ClientID %>").val("");

Also in your code-behind try this

 if (String.IsNullOrEmpty(txtDeathDate.Text) && DatePattern.IsMatch(txtDeathDate.Text))
 {

    Demo.DeathDate = txtDeathDate.Text;
    Demo.RealDeathDate = Convert.ToByte("1");
 }

Finally, put a break point and debug your code and see the values of your textbox and variables. Hope this helps!




回答2:


  1. textbox.Text from code behind will always have a value (String.Empty), even when empty. It won't be null
  2. you cant reference by ID like that because the runat=server will change the value. you'll need to do something like YOURTEXTBOX.ClientID as in $("#<%= YOURTEXTBOX.ClientID %>")

edited to show the actual code of how to ref. via ClientID




回答3:


Try this one, replace your code with following...

$("#" + "<%=txtbox.CliendID%>").val("");

This will be work....



来源:https://stackoverflow.com/questions/13329056/how-to-clear-server-textbox-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!