如何将文本框id作为参数传递给javascrip中的函数 [英] how to pass textbox id as a parameter to the function in javascrip

查看:66
本文介绍了如何将文本框id作为参数传递给javascrip中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


我已经编写了一个javascript函数来验证asp.net中的数字

我想在4个文本框中调用相同的函数。

hi
I have written a javascript function for validation of numbers in asp.net
I want to use same function to be called on 4 textboxes.

<script type="text/javascript"  language="javascript">
    function Isnumber(txt)   
    {
    var validcharacter="0123456789.";
    var char1;

   var textbox= document.getElementById("txt");
   

    var stext= textbox.value;
    for(i=0; i<=stext.length;i++)
    {
    
    char1= stext.charAt(i);
    
    if(validcharacter.indexOf(char1)==-1 )
    {
    
     alert("please enter only numbers");
     document.getElementById("txt").value="";
     return false;
    }
     
    }
    
    }




<asp:TextBox ID="txtHRA"  runat="server"
önKeyPress="javascript:Isnumber('<%= txtHRA.ClientID%>')"/>



不工作



txt是无法获取调用函数时传递的文本框ID的函数。 br />
我必须为多个文本框使用相同的功能如何将texbox id传递给javascript中的函数


its not working

txt is the function not able to get textbox id which i have passed while calling function.
I have to use the same function for multiple textbox how can i pass texbox id to the function in javascript

推荐答案

可能你开始编程了最近,但问题是:尽管典型的应用程序代码很简单,但JavaScript实际上很难理解。



而不是传递 id ,您可以传递控件对象本身。让我举一个简单的例子,仅使用HTML和JavaScript:

Probably, you started programming very recently, but the problem is: JavaScript is in fact very hard to understand, despite the simplicity of typical application code.

Instead of passing the id, you can pass the control object itself. Let me show a simple example, with HTML and JavaScript only:
<html>
    <head>
        <script type="text/javascript">
            function doSomethingWithTextBox(textBox) {
                alert(textBox.value);
            }
        </script>
    </head>
<body>

<input type="text" onkeydown="doSomethingWithTextBox(this)" />
<p>Some other text box:</p>
<input type="text" onkeydown="doSomethingWithTextBox(this)" />

</body>
</html>





如您所见,所需参数将作为此在活动宣言中。







当你有不同的文本框时,相同的处理程序仍会区分它们,因为对象实例不同。

问题解决了!

<你的webform控件上的

-SA



As you can see, the required parameter is passed as "this" in the event declaration.



And when you have different text boxes, the same handler will still distinguish them, because "this" object instances are different.
Problem solved!

—SA


,就像

on your webform control, it would be something like
attribute.add("onBlur="doSomethingWithTextBox(this); return false;)





尝试谢尔盖的第一个,我使用属性,不知道控件会接受javascript事件。但是等等,他正在使用常规的html元素。因此,如果您使用webcontrol,我认为属性会注入属性并完成工作。



Try Sergey's first, I use attribute, didn't know the control would accept javascript events. But wait, he's using a regular html element. so if your using a webcontrol, I think attributes would inject the attribute and do the job.


// Javascript函数



// Javascript function

function fnAllowDigitsOnly(key)
{      
    var keycode=(key.keywhich)?key.which:key.keyCode;
    if(keycode<48||keycode>57 || keycode !=8)
    {
        return false;
    }
}



// .cs用于调用javascript函数的代码


// .cs code for calling javascript function

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       // for each textbox u should call same function 
       text1.Attributes.Add("onkeypress", "return fnvalidatedigit(event)");
       text2.Attributes.Add("onkeypress", "return fnvalidatedigit(event)");
    }
}


这篇关于如何将文本框id作为参数传递给javascrip中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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