无法清除文本框 [英] unable to clear textbox

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

问题描述

我使用下面的代码使用.清除文本框,但文本框未清除

i used below coding to clear textbox using ., but textbox not getting clear

<script type="text/javascript" language="javascript">
    function clear()
   {
 document.getElementById("<%= txtfname.ClientID %>").value = "";
 document.getElementById("<%= txtlname.ClientID %>").value = "";
   }
   </script>


<asp:Button ID="clear" runat="server" Text="clear" CausesValidation="false"
                        BackColor="#FF8000" Font-Bold="True" OnClientClick="clear()" />

推荐答案

从我一直看到的情况来看,该函数不是事件调用.这就是我正在工作的内容:

标记:
From what I have been seeing the function isn''t event called. This is what I got working:

Markup:
<asp:textbox id="txtfname" runat="server" />
<asp:textbox id="txtlname" runat="server" />
<asp:button id="clear" runat="server" text="clear" causesvalidation="false"

     BackColor="#FF8000" Font-Bold="True"

     OnClientClick="return clearTextBoxes(this.id)" />
</asp:button>



这里需要注意的几件事:
1.我更改了函数的名称.这是因为如果我将其更改为"clear",则不会调用该函数.由于命名冲突,我正在猜测.
2.返回函数的返回值.
3.我正在向函数传递按钮的ID.这是使用javascript而不是ASP获取元素ID.

JavaScript:



A few things to note here:
1. I changed the name of the function. This is because if I were to change it to "clear" the function would not be called. I''m guessing due to a naming conflict.
2. I return the return value of the function.
3. I am passing the function the id of the button. This is to obtain the element ids using javascript instead of ASP.

JavaScript:

function clearTextBoxes(btnId) {
    var selector = btnId.replace("clear", "txtfname");
    var fname = document.getElementById(selector);

    selector = btnId.replace("clear", "txtlname");
    var lname = document.getElementById(selector);
    fname.value = "";
    lname.value = "";
    return true;
}



在这里,我创建要在getElementById中使用的第一个文本框的ID.然后,我得到一个包含第一个文本框的局部变量.对第二个文本框执行相同的操作.获取文本框后,即可开始清除.最后,我返回true.相反,返回"false"将阻止发回邮件.返回"true"会使帖子回退.



Here I create the id of the first textbox to use in getElementById. Then I get a local variable containing the first textbox. The same thing is done for the second textbox. After the textboxes are obtained, clearing can commence. Finally I return true. Returning "false" instead would keep from a post back. Returning "true" keeps the post back.


嘿,伙计,您的代码运行良好.其实已经给了同样的明确.那不是保留关键字,而是由DOM使用.就像document.clear.
所以最好重命名.
其次,您正在尝试清除按钮文本.但是您正在提交表单,它的意思是重新发布时的意思.它会重新加载您的价值.
这些是我在您的代码中所做的一些修改.它的工作情况很好检查


Hey buddy your code is working well. actually have given same clear. that is not reserved keyword but its use by DOM. like document.clear.
so better rename it.
second, you are trying to clear button text. but you are submitting form, its mean when its relaod.. it reload its value thou.
These are some modification i have done in your code. its working well check it


<asp:textbox id="txtfname" runat="server" xmlns:asp="#unknown" />
   <asp:textbox id="txtlname" runat="server" xmlns:asp="#unknown" />
<asp:button id="clear" runat="server" text="clear" causesvalidation="false" xmlns:asp="#unknown">
                        BackColor="#FF8000" Font-Bold="True" OnClientClick="clearid()" /></asp:button>




脚本在这里..




script is here..

<script type="text/javascript">
   // function clearid() {
    //   return false;
   // }
    function clearid() {
        document.getElementById("<%= txtfname.ClientID %>").value = "";
        document.getElementById("<%= txtlname.ClientID %>").value = "";
    }

</script>



尝试这个. :-)



try this. :-)


这篇关于无法清除文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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