简体   繁体   English

无法在javascript中设置asp.net文本框值

[英]unable to set asp.net textbox value in javascript

I'm trying to create a simple counter box next to an ASP.NET multi-line textbox. 我正在尝试在ASP.NET多行文本框旁边创建一个简单的计数器框。 I'm using JavaScript to do this ClientSide. 我正在使用JavaScript来执行此ClientSide。

 <script type="text/javascript">

       function textCounter(maxlimit) {

           var txtField = document.getElementById('<%=txtWhatCompanyDoes.ClientId%>')

           var cntCounter = document.getElementById('<%=txtWhatCompDoesChrsRem%>')

           if (txtField.value.length > maxlimit) {

               txtField.value = txtField.value.substring(0, maxlimit);
           }
           else
           {
               var chrsleft = maxlimit - txtField.value.length;
               cntCounter.value = chrsleft;

           }
       }
    </script>

My page code is; 我的页面代码是;

<asp:TextBox ID="txtWhatCompanyDoes" onKeyUp="textCounter('txtWhatCompanyDoes', 'txtWhatCompDoesChrsRem',2000);" runat="server" TextMode="MultiLine" Rows="10"></asp:TextBox>
                <asp:TextBox ID="txtWhatCompDoesChrsRem" runat="server"></asp:TextBox>

The problem is that it works right up to the last line of the JavaScript as I can use an alert to tell me the number of characters left, but's just not writing the value to the textbox. 问题在于它可以一直运行到JavaScript的最后一行,因为我可以使用警报来告诉我剩余的字符数,但是并没有将值写入文本框。 Am I doing something wrong? 难道我做错了什么?

Try adding client id on line 4 at the end of txtWhatCompDoesChrsRem 尝试在txtWhatCompDoesChrsRem末尾的第4行添加客户端ID

       function textCounter(maxlimit) {

           var txtField = document.getElementById('<%=txtWhatCompanyDoes.ClientId%>')

           var cntCounter = document.getElementById('<%=txtWhatCompDoesChrsRem.ClientId%>')

           if (txtField.value.length > maxlimit) {

               txtField.value = txtField.value.substring(0, maxlimit);
           }
           else
           {
               var chrsleft = maxlimit - txtField.value.length;
               cntCounter.value = chrsleft;

           }
       }
    </script>

Wow, how emabarassing. 哇,真令人讨厌。 I can't believe I missed that. 我简直不敢错过。 Can I be cheeky and ask how this can be made dynamic for multiple counter boxes on one page eg 我可以厚脸皮吗,请问如何在一页上的多个计数器盒中使它动态化,例如

<script type="text/javascript">

   function textCounter(field, cntField, maxlimit) {

       var txtField = document.getElementById(field)

       var cntCounter = document.getElementById('<%=txtWhatCompDoesChrsRem.Clientid%>')

       if (txtField.value.length > maxlimit) {

          txtField.value = txtField.value.substring(0, maxlimit);
       }
       else
       {
          var chrsleft = maxlimit - txtField.value.length;

          cntCounter.value = chrsleft;

       }
    }
    </script>




<asp:TextBox ID="txtWhatCompanyDoes" onKeyUp="textCounter(this.id, txtWhatCompDoesChrsRem',2000);" runat="server" TextMode="MultiLine" Rows="10"></asp:TextBox>
    <asp:TextBox ID="txtWhatCompDoesChrsRem" runat="server"></asp:TextBox>

I can pass the first control id using "this.id" but how can I pass the counter box as a variable? 我可以使用“ this.id”传递第一个控件ID,但是如何将计数器框作为变量传递?

This only way I could solve this was to set the counter box as an rather than an ASP textbox. 我可以解决此问题的唯一方法是将计数器框设置为ASP而不是ASP文本框。

<asp:TextBox ID="txtWhatCompanyDoes" onKeyUp="textCounter(this.id, this.form.txtWhatCompDoesChrsRem, 2000);" runat="server" TextMode="MultiLine" Rows="10"></asp:TextBox>
 <input name="txtWhatCompDoesChrsRem" type="text" value="2000" />

every element in asp.net have inherit id like "txtWhatCompDoesChrsRem.Clientid" asp.net中的每个元素都具有继承ID,例如"txtWhatCompDoesChrsRem.Clientid"

so when u want to use some element in java script u can simply add one line to the element : 因此,当您想在Java脚本中使用某些元素时,只需在元素中添加一行即可:

<asp:TextBox id="txt1" ClientID="static" ... 

thats mean u can use the element directly by his name 多数民众赞成意味着你可以直接用他的名字使用元素

java script : Java脚本:

var txtField = document.getElementById("txt1")

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

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