[英]Simple Javascript function is not working on .ascx file
我正在嘗試在asp.net和vb.net應用程序的.ascx文件中使用簡單的javascript函數。 我在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://tcmypeoplebiz.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) {
...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.