我正在驗證JavaScript的表單。 目標是:如果用戶未在必填字段中輸入值,則會顯示錯誤消息並在幾秒鐘後消失。 但是 問題是:錯誤消息不顯示,這可能是因爲從JavaScript我沒有寫錯誤消息在標籤控制。但是,如果我使用JavaScript警報而不是顯示標籤,它的工作原理。無法顯示來自javascript的標籤
代碼: 的Javascript
<script type="text/javascript">
function showConfirmation() {
$('div#confirmationDV').show();
setTimeout(function() {
$('div#confirmationDV').fadeOut(5000);
});
}
function validateRequiredField() {
if (document.getElementById("<%=txtOfferTitle.ClientID%>").value == "") {
alert("error.....");//this alert works.
document.all("<%=lblConfirmation.ClientID%>").innerHTML = "please enter your business name"; // this does not work
document.all("<%=lblConfirmation.ClientID%>").style.color = "red";
showConfirmation();
document.getElementById("<%=txtOfferTitle.ClientID%>").focus();
return false;
}
return true;
}
</script>
HTML
<div class="round-conf-box" id="confirmationDV">
<div class="round-conf-tl">
<div class="round-conf-tr"></div>
<div class="round-conf-background_color_top"></div>
</div>
<div class="round-conf-box-Content">
<asp:Label ID="lblConfirmation" runat="server" CssClass="confirmationLabel"></asp:Label>
</div>
<div class="round-conf-bl">
<div class="round-conf-br"></div>
<div class="round-conf-background_color_bottom"></div>
</div>
//...some其他的東西
<asp:ImageButton ID="iBtnSave" runat="server" ImageUrl="~/images/createOffer.png"
onclick="iBtnSave_Click" OnClientClick="return validateRequiredField()" />
,並在後面
代碼210protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
iBtnSave.Attributes.Add("onclick", "return validateRequiredField()");
//some other stuff
}}
將是很好,如果有人能幫助我找到我的錯誤.. 歡呼
是您的驗證在客戶端或服務器端做了什麼?如果客戶端那麼你爲什麼在驗證函數中使用asp標籤? – AmGates
它在客戶端。在validateRequiredField()函數內部。你在說什麼asp標籤? – kandroid
document.all(「<%= lblConfirmation.ClientID%>」)。innerHTML =「請輸入您的商家名稱」; //這不起作用 document.all(「<%= lblConfirmation.ClientID%>」)。style.color =「red」;這些標籤將被替換在服務器端na?您是否通過查看瀏覽器中顯示的頁面源代碼來檢查值? – AmGates