如何将值传递到onClientClick的aspx页面中的javascript函数 [英] How to pass a value into a javascript function within an aspx page onClientClick

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

问题描述

我想在回发之前检查文本框值。我将 onClientClick 值设置为我的函数,但我不知道如何传递数据进行检查,在这种情况下我想检查txt1输入的文本。

I want to check the textbox value before postback occurs. I set the onClientClick value to my function but I don't know how to pass the data to check, in this case I want to check the txt1 entered text.

<asp:textbox id="txt1" runat="server />

<asp:LinkButton ID="LinkButton1"
                runat="server"
                Text="Save" 
                onclick="Save"
                OnClientClick="javascript:check(WANT TO PUT TEXTBOX VALUE HERE);"
                />

我的javascript:

My javascript:

function check(txt) {
 var pattern = /^[0-9]{1,11}(,[0-9]{0,2})?$/;
 if (!pattern.test(txt)) {
    return false;
 }
 else {
    return true;
 }
}

问题是这个检查功能是在按键事件中使用的txt1所以我无法使用:

The issue is that this check function is used along the keypress event of the txt1 so I cant use:

function check(){
    var txt=$('#txt1').val();
}

整个JS代码:

$('#txt1').keypress(function (e) {
    var key = String.fromCharCode(e.which);
    var txt = $(this).val() + key;
    if (check(txt)) {
        // do sth
    } else {
        e.preventDefault();
        // do sth
    }
});

如何告诉OnclientCLick函数获取txt1值?

How can I tell the OnclientCLick function to get the txt1 value?

谢谢。

推荐答案

类似的东西:

<asp:textbox id="txt1" runat="server />

<asp:LinkButton ID="LinkButton1"
                runat="server"
                Text="Save" 
                onclick="Save"
                OnClientClick="check(document.getElementById('<%=txt1.ClientID%>').value;)"
                />






修改:

其他方法:



Other methods :


  1. 您可以在服务器端添加它: LinkBut​​ton1.OnClientClick =check(document.getElementById('txt1.ClientID +')。value;);

如果你想留在aspx页面,你必须在OnClientClick中添加一个javascript函数的名称并实现javascript函数在脚本标记中:

If you want to remain in the aspx page, you have to put a name of a javascript function in the OnClientClick and implement the javascript function in a script tag :

<asp:LinkButton ID="LinkButton1"
                runat="server"
                Text="Save" 
                OnClientClick="validate();"
                />
<script type="text/javascript">
    function validate() {
        alert(document.getElementById('<%=txt1.ClientID%>').value);
        return false;
    }
</script>


这篇关于如何将值传递到onClientClick的aspx页面中的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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