2011-10-13 86 views
0

我正在驗證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()" /> 

,並在後面

代碼210
protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!IsPostBack) 
     { 
      iBtnSave.Attributes.Add("onclick", "return validateRequiredField()"); 
//some other stuff 
}} 

將是很好,如果有人能幫助我找到我的錯誤.. 歡呼

+0

是您的驗證在客戶端或服務器端做了什麼?如果客戶端那麼你爲什麼在驗證函數中使用asp標籤? – AmGates

+0

它在客戶端。在validateRequiredField()函數內部。你在說什麼asp標籤? – kandroid

+0

document.all(「<%= lblConfirmation.ClientID%>」)。innerHTML =「請輸入您的商家名稱」; //這不起作用 document.all(「<%= lblConfirmation.ClientID%>」)。style.color =「red」;這些標籤將被替換在服務器端na?您是否通過查看瀏覽器中顯示的頁面源代碼來檢查值? – AmGates

回答

1

你混合jQuery和IE-只有JScript中。

確保只使用jQuery或跨瀏覽器的JavaScript

變化

function validateRequiredField() { 
     if (document.getElementById("<%=txtOfferTitle.ClientID%>").value == "") { 
      alert("error....."); 
      document.all("<%=lblConfirmation.ClientID%>").innerHTML = "please enter your business name"; 
      document.all("<%=lblConfirmation.ClientID%>").style.color = "red"; 
      showConfirmation(); 
      document.getElementById("<%=txtOfferTitle.ClientID%>").focus(); 
      return false; 
     } 
     return true; 
    } 

$("#<%=iBtnSave.ClientID%>).click(function(e) { 
    var txtOffer = $("#<%=txtOfferTitle.ClientID%>"); 
    var txtOfferLbl = $("#<%=lblConfirmation.ClientID%>"); 
    if (txtOffer.val() == "") { 
    alert("error....."); 
    txtOfferLbl.text("please enter your business name"); 
    txtOfferLbl.addClass("error"); 
    showConfirmation(); 
    txtOffer.focus(); 
    e.preventDefault(); 
    } 
    else { 
    txtOfferLbl.text(""); 
    txtOfferLbl.removeClass("error"); 
    } 
}); 
+0

感謝您的回答。但它不工作。我是否需要在page_load事件中添加任何內容?只是爲了您的信息,我的代碼在Chrome和IE中工作,但它不適用於Firefox。任何進一步的幫助將不勝感激。 :) – kandroid

+0

不看實際的頁面及其渲染輸出,我不能保證我的代碼工作,因爲我已經承擔了很多事情。 例如,您需要一種樣式:'.error {color:red}' – mplungjan

+0

@kandroid您是否可以返回以評論我的擔憂? – mplungjan