如何使用jQuery清除服务器文本框 [英] how to clear server textbox using jquery

查看:92
本文介绍了如何使用jQuery清除服务器文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用客户端功能来清除文本框(服务器控件runat ="server"),因此当我使用jquery清除文本框时,它似乎为空,但是当我跟踪代码并检查textbox.Text控件时,我在那里找到了值而不是null,因此如何从客户端也从文本框控件的value属性中清除它(我必须从客户端清除它以进行用户交互)

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)

我正在使用以下内容从客户端代码中清除它:

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

这是我控制的代码:

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

在后面的代码中:

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

在萤火虫跟踪处:

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

并且当用户通过(事件点击)更改复选框的值时,我正在调用javascript代码

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;
                }
            }
        }

    }

推荐答案

您应该使用它来获取服务器控件的客户端ID

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!

这篇关于如何使用jQuery清除服务器文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆