如何在按钮点击事件上调用javascript函数 [英] How to Call javascript function on button click event

查看:162
本文介绍了如何在按钮点击事件上调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在asp.net应用程序中使用了两个文本框。我想在buuton点击上比较这两个值。如何检查两个文本框值并使用javascript显示警告消息以及如何在按钮点击事件上调用该功能。





如果有的话一个人知道这个帮助我。



问候

Nanda Kishore.CH

Hi,
I used Two textboxes in my asp.net application. i want to compare that two values on buuton click. How to Check that two textbox values and display alert message using javascript and how to call that function on button click event.


If any one know about this Help me.

Regards
Nanda Kishore.CH

推荐答案

试试这个...它很简单



Try this...its so simple

<script type="text/javascript" language="javascript">
 function ValidateForm()
    {
           var Form=document.getElementById('frmDemo');
           alert(Form.txtbox1.value);
           alert(Form.txtbox2.value);
           if(parseFloat(Form.txtbox1.value)<parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
//or
            if(parseFloat(Form.txtbox1.value)==parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
//or
            if(parseFloat(Form.txtbox1.value)<parseFloat(Form.txtbox2.value))
            {
                alert('hello all');
                document.getElementById('txtbox1').focus();
                return false;
            }
    }

</script>





致电<$ c按钮 OnClientClick



Call ValidateForm() on Button OnClientClick

<asp:Button ID="BtnSubmit" Text="Proceed" runat="server" OnClientClick="return ValidateForm();"/>







谢谢

Ashish



标记解决方案这是你的答案




Thanks
Ashish

Mark it solution if this is your answer


在asp按钮中有一个名为OnClientClick的属性你可以指定java脚本函数,如下所示



In asp button there is a property called OnClientClick there u can specify the java script function like given below

<asp:button id="btnSave" tabIndex="32" runat="server" CssClass="btn" OnClientClick = "return compare()"Text="Save"></asp:button>





在你的javascipt函数中你现在可以编写如下给出的比较函数





in ur javascipt function now u can write the compare function like given below

<script language="javascript" type="text/javascript">


            function compare() {

var txt1 = document.getElementById('txt1').value; // textbox 1 ID is txt1
var txt2 = document.getElementById('txt2').value; // textbox 2 ID is txt2

                if (txt1 == txt2) {
                    alert("both are same");
                }
                else {
                    alert("both are different");
                }

            }

        </script>







检查一下。




Check this out.


使用这个:



USe This:

<asp:button id="Btn_submit" onclientclick="return validate()" onclick="Btn_submit_Click()"/>





这里OnClientClick用于调用javascript函数。这里验证是一个javascript函数。





Here The OnClientClick is used for calling the javascript function. Here validate is a javascript function.

<script type="text/javascript">
function vaildate()
{
...................
...................
....................
}
</script>


这篇关于如何在按钮点击事件上调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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