在asp.net中执行JS函数后,TextBox的颜色值消失了吗?

时间:2018-08-22 10:21:05

标签: asp.net xhtml

通过下面的JS函数设置TexBox的color属性时,该属性消失(闪烁)。请指教,如何保留颜色属性?

(即,一旦单击按钮,颜色(绿色代表奇数,红色代表偶数)闪烁,颜色又变回空白)

function numbercheck() {
  var num = document.getElementById("txtnum").value;

  if (num == "") {
    alert("Please Enter number")
  } else {
    if (num % 2 == 0) {
      alert("You have entered even number");
      document.getElementById("txtnum").style.backgroundColor = "green";
    } else {
      alert("You have enetered Odd number");
      document.getElementById("txtnum").style.backgroundColor = "red";
    }
  }
}
<p> Please enter a Number :
  <asp:TextBox ID="txtnum" runat="server"></asp:TextBox>
</p>
<asp:Button Text="Check Number" ID="btnnum" runat="server" OnClientClick="return numbercheck()" />

1 个答案:

答案 0 :(得分:0)

    Please change your script function as
      function numbercheck() {
                debugger;
                var num = document.getElementById("<%=txtnum.ClientID%>").value;

                if (num == "") {
                    alert("Please Enter number")
                } else {
                    if (num % 2 == 0) {
                        alert("You have entered even number");
                        document.getElementById("<%=txtnum.ClientID%>").style.backgroundColor = "green";
                    } else {`enter code here`
                        alert("You have enetered Odd number");
                        document.getElementById("<%=txtnum.ClientID%>").style.backgroundColor = "red";
                    }
                }
            }

and 
<asp:Button Text="Check Number" ID="btnnum" runat="server" OnClientClick="return numbercheck()"  CausesValidation="false" UseSubmitBehavior="false" />