简单的Javascript函数不工作.ascx文件

问题描述:

我想在我的asp.net和vb.net应用程序中的.ascx文件中工作一个简单的JavaScript函数。我在asp面板标签中包含了下面的html代码。简单的Javascript函数不工作.ascx文件

    <div class="inline" > 

        <asp:CheckBox ID="tick" runat="server" Text="Please tick the box to confirm the above information are correct" Checked="false" CssClass="small" onchange="EnableSubmit()" /> 
       </div> 

,并在.ascx文件

<script type="text/javascript"> 
function EnableSubmit() { 

    if (document.getElementById("tick").checked == true) { 


     document.getElementById("uxSubmit").enabled = true; 
    } 
    else { 
     document.getElementById("uxSubmit").enabled = false; 
    } 


} 

你可以从我试图包含一个复选框代码中看到下面的JavaScript代码。用户必须勾选复选框才能使提交按钮处于活动状态。但该功能不起作用。每当我勾选复选框时,屏幕前方会出现一个白色框(如popoup)。

我已经尝试了这个链接给出的答案,但以这种方式我的整个形式变得无形。 How to use javascript in .ascx pages

我很感谢您的建议和帮助。

谢谢

在.ascx文件的完整代码给出。请到让我知道如果你需要更多的信息。

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CandidateRegistration3.ascx.vb" 
Inherits="_controls_CandidateRegistration3" %> 
<asp:CustomValidator ID="uxProfileCVValReq" runat="server" EnableViewState="false" 
ErrorMessage="You create a 'Personal Profile' or upload a Word Compatible or PDF 'Curriculum Vitae'." 
ValidationGroup="candidateregistration" Display="None" /> 

<div style="margin-bottom: 20px;"> 
<asp:Panel ID="panUpload" runat="server"> 
    <asp:CustomValidator ID="CustomValidator1" runat="server" EnableViewState="false" 
     ErrorMessage="You must upload a Word Compatible or PDF 'Curriculum Vitae'." Display="None" 
     ValidationGroup="CVUpload" /> 
    <p>Your registration form will be populated from the details contained in your CV or application form, please upload now.</p> 
    <asp:FileUpload ID="txtFilePath" runat="server" CssClass="" ValidationGroup="CVUpload" /> 

    <asp:HiddenField ID="hdDocId" runat="server" /> 
</asp:Panel> 
<asp:Panel ID="panForm" runat="server" Visible="false"> 

    <table class="table table-bordered"> 

     <tr> 
      <td><label>Email Address</label></td> 
      <td> 
       <asp:TextBox ID="uxEmail" runat="server" CssClass="" MaxLength="100" 
        EnableViewState="False" /> 

      </td> 
     </tr> 
     <tr> 
      <td><label>Password</label></td> 
      <td> 
       <asp:TextBox ID="uxPassword" runat="server" CssClass="" MaxLength="20" 
        EnableViewState="False" TextMode="Password" /> 
      </td> 
     </tr> 
     <tr> 
      <td><label>Confirm Password </label></td> 
      <td> 
       <asp:TextBox ID="uxPasswordConfirm" runat="server" CssClass="" TextMode="Password" /> 
      </td> 
     </tr> 
    </table> 

    <asp:CustomValidator ID="uxCVFileTypeReq" runat="server" EnableViewState="false" 
     ErrorMessage="You must upload a Word Compatible or PDF 'Curriculum Vitae'." Display="None" 
     ValidationGroup="CVUpload" /> 
    <asp:HiddenField ID="uxCVID" runat="server" EnableViewState="false" /> 

    <p>To complete your registration please click Next.</p> 

    <div class="inline" > 

     <asp:CheckBox ID="tick" runat="server" Text="Please tick the box to confirm the above information are correct" Checked="false" CssClass="small" onchange="EnableSubmit()" /> 

    </div> 

</asp:Panel> 
</div> 

    <p class=""> 
     <asp:LinkButton runat="server" ID="uxSubmit" Text="Next" OnClick="SaveClick" CausesValidation="true" ValidationGroup="candidateregistration" CssClass="button2" Enabled="false" /> 

     <a href="#" onclick="parent.$.colorbox.close()" class="button2">Cancel</a> 
     <asp:LinkButton runat="server" ID="uxUpload" Text="Upload" CssClass="button2" CausesValidation="true" ValidationGroup="CVUpload" /> </p> 


<script type="text/javascript"> 
    function EnableSubmit() { 

     if (document.getElementById("tick").checked == true) { 


       document.getElementById("uxSubmit").enabled = true; 
      } 
     else { 
      document.getElementById("uxSubmit").enabled = false; 
      } 


     } 
</script> 

理解这个问题更好的,我讲的,看看我的网页直播请到以下链接。 http://t.c.mypeoplebiz.com/sagepay-careers/2935-commercial-litigator-solicitor-1-3-years-pqe.aspx

然后单击立即应用按钮。

将出现一个弹出窗口。请给出一个电子邮件地址进行注册。 (不用担心,它不必是一个原始的电子邮件地址,只需[email protected]就可以)。

当您上传文档时,您可以看到该表单。复选框位于页面的底部。

在您的浏览器中,右键单击该页面,单击查看源代码(或者您为浏览器执行此操作)。注意到ID已经改变了?这使得它没有找到你的元素来启用它们。

<script type="text/javascript"> 
function EnableSubmit() { 
    if (document.getElementById('<%= tick.ClientId %>').checked == true) { 
     document.getElementById('<%= uxSubmit.ClientId %>').enabled = true; 
    } 
    else { 
     document.getElementById('<%= uxSubmit.ClientId %>').enabled = false; 
    } 
} 
</script> 

一个解决方案是将客户端ID嵌入到带有内联服务器端表达式的脚本中。另一种是更改ClientIDMode。将ClientIdMode设置为static将使客户端在客户端和服务器上使用相同的ID,从而避免需要内联表达式。

+0

感谢您的回复。我已经尝试了你的建议方法。但仍然没有工作。该文件不包含任何主文件引用。但是当我右键单击并检查它的元素\我可以看到id名称已被更改。 – Bashabi 2015-02-06 16:27:21

+0

@Bashabi客户端上的ID是否与服务器上的ID匹配? (您是否在客户端ID上看到uxSubmit或其他内容?) – mason 2015-02-06 16:28:36

+0

要更好地理解我正在讨论的问题并查看我的网页,请转到以下链接。 http://t.c.mypeoplebiz.com/sagepay-careers/2935-commercial-litigator-solicitor-1-3-years-pqe.aspx 然后单击立即应用按钮。 将出现一个弹出窗口。请给出一个电子邮件地址进行注册。 (不用担心,它不必是一个原始的电子邮件地址,只需[email protected]就可以)。 当您上传文档时,您可以看到表单。复选框位于页面的底部。 – Bashabi 2015-02-06 16:37:24

ASP.NET对设置为runat =“server”的任何内容的ID进行了修改。您需要询问该控件的实际ID。如在:

function EnableSubmit() { 

    var id = "<%= tick.ClientID %>"; 
    if (document.getElementById(id).checked == true) { 

     ... 
    } 


} 
+0

您缺少ID中的引号。 – mason 2015-02-06 16:14:50