简单的Javascript函数在.ascx文件上不起作用

Simple Javascript function is not working on .ascx file

本文关键字:文件 不起作用 ascx Javascript 函数 简单      更新时间:2023-09-26

我正在尝试让一个简单的javascript函数在我的asp.net和vb.net应用程序的.ascx文件中工作。我在asp面板标记中包含了以下html代码。

                    <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文件中的以下javascript代码

<script type="text/javascript">
function EnableSubmit() {
    if (document.getElementById("tick").checked == true) {

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

}

正如您从代码中看到的,我正在尝试包含一个复选框。用户必须勾选复选框才能激活提交按钮。但该功能不起作用。每当我勾选复选框时,屏幕前就会出现一个白色框(像popoup一样)。

我试过这个链接中给出的答案,但就这样我的整个身体都看不见了。如何在.ascx页面中使用javascript

我感谢你的善意建议和帮助。

谢谢

.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

然后单击"立即应用"按钮。

将出现一个弹出窗口。请提供注册的电子邮件地址。(别担心。它不一定是原始的电子邮件地址。只是abc@xyz.com很好)。

当你上传文档时,你可以看到表单。复选框在页面底部。

在浏览器中,右键单击页面,然后单击"查看源代码"(或者按照您的浏览器进行查看)。注意到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,从而避免了内联表达式的需要。

ASP.NET会破坏任何设置为runat="server"的ID。您需要向它询问该控件的实际id。如:

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

}