javascript没有执行

时间:2015-11-03 18:02:54

标签: javascript jquery asp.net vb.net

我有一个javascript函数,可以计算输入asp:TextBox的字符数。

我将其设置为15个字符进行测试并在其中放入alertBox,但它不起作用。

JavaScript根本没有执行。

我尝试将字段名称放在<%= %>块中,但代码仍然没有触发。

这是我的代码:

Asp Code

<%@ Page Title="Contact Us" Language="vb"  MasterPageFile="~/Site.Master" AutoEventWireup="false" CodeBehind="Contact.aspx.vb" Inherits="HosJun.Contact" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
   <script src="Scripts/jquery-1.4.1.js"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <table width="900" border="0" cellspacing="0" bordercolor="#333366" >
      <tr>
         <td width="900"  valign="top" >
            <div id="middle">
            <div id="middlebox2">
            <table width="96%" border="0" align="center" cellpadding="3" cellspacing="3">
            </table>
            <form id="form1" >
               <table align="center" style="width: 322px">
                  <tr>
                     <td>
                        <asp:Label ID="lblContactMainName" class="lblC" runat="server" Text="Your Name:"></asp:Label>
                     </td>
                     <td class="auto-style4">
                        <asp:TextBox ID="txtContactMainName" runat="server" Width="210px"></asp:TextBox>
                     </td>
                     <td align="left">
                        <asp:RequiredFieldValidator ID="valContactName" runat="server" ErrorMessage="You must enter a contact name" Font-Bold="True" ForeColor="#E46A18" ControlToValidate="txtContactMainName">*</asp:RequiredFieldValidator>
                     </td>
                  </tr>
                  <tr>
                     <td>
                        <asp:Label ID="lblContactMainEmail"  class="lblC" runat="server" Text="Your Email:"></asp:Label>
                     </td>
                     <td class="auto-style4">
                        <asp:TextBox ID="txtContactMainEmail" runat="server" Width="210px" ControlToValidate="txtContactMainEmail"></asp:TextBox>
                     </td>
                     <td align="left">
                        <asp:RequiredFieldValidator ID="valContactEmail" runat="server" ErrorMessage="Please enter a valid email address" Font-Bold="True" ForeColor="#E46A18" ControlToValidate="txtContactMainEmail">*</asp:RequiredFieldValidator>
                     </td>
                  </tr>
                  <tr>
                     <td>
                        <asp:Label ID="lblContactMainSubject"  class="lblC" runat="server" Text="Subject:"></asp:Label>
                     </td>
                     <td class="auto-style4">
                        <asp:DropDownList ID="ddlContactMainSubject" runat="server" Width="215px">
                           <asp:ListItem>FeedBack on a room</asp:ListItem>
                           <asp:ListItem>FeedBack on the Site</asp:ListItem>
                           <asp:ListItem>Suggest a Site</asp:ListItem>
                           <asp:ListItem>Other</asp:ListItem>
                        </asp:DropDownList>
                     </td>
                  </tr>
               </table>
               <table align="center" style="width: 364px">
                  <tr>
                     <td align="left"><span>Comment:</span></td>
                  </tr>
                  <tr>
                     <td align="left" >
                        <asp:TextBox ID="txtComment" runat="server" TextMode="MultiLine" Height="70px" Width="356px"></asp:TextBox>
                        <br />  
                        (<span id="spanComment">15 Characters left</span>)  
                     </td>
                  </tr>
                  <tr>
                     <td align="center" class="auto-style3">
                        <asp:Label ID="lblCaptchaResult" runat="server" Width="356px" Text="Enter the CAPTCHA code below." Height="18px"></asp:Label>
                     </td>
                  </tr>
                  <tr>
                     <td align="center" valign="middle" class="auto-style3">
                        <center style="width: 357px">
                           <asp:TextBox runat="server" ID="txtImg" width="90" valign="top" style="margin-left: 0px" ></asp:TextBox>
                           <asp:Image ID="imgCaptcha" runat="server" valign="top" ></asp:Image>
                        </center>
                     </td>
                  </tr>
                  <tr>
                     <td align="center" class="auto-style3">
                        <center style="width: 355px">
                           <asp:Button ID="Button1" runat="server" autopostback="true" Text="Submit" />
                        </center>
                     </td>
                  </tr>
               </table>
   </table>
   </form>
</asp:Content>

JavaScript代码

<script src="Scripts/jquery-1.4.1.js"></script>
   <script type="text/javascript">
      //Checking Description Length  
      $('txtComment').keyup(function () {
          alert('Alert');
          var Description = $('txtComment').val();
          var Descriptionlen = Description.length;
          if (Description.length >= 15) {
              this.value = this.value.substring(0, 15);
          }
          $('#spanComment').text(15 - Descriptionlen + ' Characters Left');
      });
      });
   </script>  

关于我做错的任何想法?这让我疯了!

4 个答案:

答案 0 :(得分:1)

您正以错误的方式获取txtComment控件。尝试使用&lt;%= txtComment.ClientID%&gt;替换txtComment的脚本

<script type="text/javascript">
$(document).ready(function() {
  $('#<%= txtComment.ClientID %>').keyup(function () {
    alert('Alert');
    var Description = $('#<%= txtComment.ClientID %>').val();
    var Descriptionlen = Description.length;
    if (Description.length >= 15) {
      this.value = this.value.substring(0, 15);
    }
    $('#spanComment').text(15 - Descriptionlen + ' Characters Left');
  });
});
</script> 

答案 1 :(得分:0)

而不是

 $('txtComment')

尝试使用:

$('<%= txtComment.ClientID %>') //UniqueID is another option

您呈现的ID与您认为在代码隐藏中的ID不同 - 打开Firebug或您的开发者控制台,您就会明白我的意思。

答案 2 :(得分:0)

如果使用id选择器,则必须使用字符“#”作为控件ID的前缀。试试

> $('#txtComment').keyup(function () {...});

答案 3 :(得分:0)

看起来您的脚本可能在dom准备好之前尝试执行。试试这个

$(document).ready(){
    //Checking Description Length  
    $('txtComment').keyup(function () {
        alert('Alert');
        var Description = $('txtComment').val();
        var Descriptionlen = Description.length;
        if (Description.length >= 15) {
            this.value = this.value.substring(0, 15);
        }
        $('#spanComment').text(15 - Descriptionlen + ' Characters Left');
    });
}