简体   繁体   English

在背后的代码中通过函数调用传递客户端ID时,无法通过aspx页面上的javascript函数访问字段的值(页面加载)

[英]Unable to access a field's value via javascript function on aspx page on passing client id from function call in code behind(page load)

I have a javascript function as follows 我有一个JavaScript函数,如下所示

function textCounter(field, rem, maxlimit) {
        if (field.value.length > maxlimit)
            field.value = field.value.substring(0, maxlimit);
        else
            rem.value = maxlimit - field.value.length;
    }

I am trying to call it from code behind by passing clien ID's 我试图通过传递clien ID来从后面的代码中调用它

ClientScript.RegisterStartupScript(this.GetType(), "myScript", "textCounter('" + txtRemark.ClientID + "','" + txtRemarksRem.ClientID + "', '2000')", true);

But I am unable to get the values of the associated fields. 但是我无法获取关联字段的值。 I am getting null value for "field.value.length" 我正在获取“ field.value.length”的空值

Let me know the issue. 让我知道这个问题。

You are passing only client Ids for the controls, but not the control, you should first get those control to work. 您仅传递控件的客户端ID,而不传递控件,则应首先使这些控件起作用。

for that you should change your code as 为此,您应该将代码更改为

function textCounter(field, rem, maxlimit) {

    var fieldCtrl = document.getElementById(field);
    var remCtrl = document.getElementById(rem);

    if (fieldCtrl.value.length > maxlimit)
        fieldCtrl.value = fieldCtrl.value.substring(0, maxlimit);
    else
        remCtrl.value = maxlimit - fieldCtrl.value.length;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM