页面加载时调用字符计数javascript函数 [英] call character count javascript function on page load

查看:59
本文介绍了页面加载时调用字符计数javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的java脚本函数:



函数LimtCharacters(txtMsg,CharLength,指标){

chars = txtMsg.value。长度;

document.getElementById(指标).innerHTML = CharLength - chars;

if(chars> CharLength){

txtMsg.value = txtMsg.value.substring(0,CharLength);

}

}



及其工作文本框的onkeyup事件如下所示:



below is my java script function :

function LimtCharacters(txtMsg, CharLength, indicator) {
chars = txtMsg.value.length;
document.getElementById(indicator).innerHTML = CharLength - chars;
if (chars > CharLength) {
txtMsg.value = txtMsg.value.substring(0, CharLength);
}
}

and its working on the onkeyup event of the text box like below :

<asp:TextBox runat="server" ID="TxtSms" Width="245px" TextMode="MultiLine" Height="57px"

                        placeholder="Max 160 Characters" BackColor="LightGray" onkeyup="LimtCharacters(this,160,'lblcount');"

                        TabIndex="1">
                    </asp:TextBox>
                    <label id="lblcount" style="color: Red;" runat="server">&nbsp; 160</label>







现在我想在页面加载时使用这个,我就是我们主孩子的概念。



所以任何帮助都会受到赞赏,如果有任何人有想法,那就很紧急。



提前感谢。




now i want to use this on page load and i am using master child concept.

so any help would be appreciated and its urgent if any one have idea with example.

thanks in advance.

推荐答案

此操作与ASP.NET无关。也许最简单的方法是:

This action has nothing to do with ASP.NET. Perhaps the simplest way would be this:
<html>
   <head>
      <title>Do something on load</title>
   </head>
<body>


<script>

document.body.onload = function() {

   DoSomethingHere();

}

</script>

</body>
</html>



剩余问题是您在问题中显示的功能。我不知道为什么你会做这样奇怪的事情,你怎么能使用硬编码常量160.但很明显,在加载时调用它,你不能使用this;你应该再次通过 ID 获取与你的文本框相关的DOM元素,然后传递它而不是this。



-SA


Remaining problem is the function you show in your question. I have no idea why would you do such weird thing and how can you use a hard-coded constant 160. But it's apparent that to call it on load, you cannot use "this"; you should, again, take the DOM element related to your text box by ID and pass it instead of "this".

—SA


这篇关于页面加载时调用字符计数javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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