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

Colour value of TextBox vanishes after JS function is executed in asp.net?

提问人:Dhivakhar Venkatachalam 提问时间:8/22/2018 更新时间:8/22/2018 访问量:28

问:

当通过以下 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()" />

asp.net xhtml

评论

0赞 VDWWD 8/22/2018
该按钮会触发回发,然后对 DOM 所做的更改将丢失。
2赞 Suprabhat Biswal 8/22/2018
OnClientClick="numbercheck(); return false;"将阻止按钮回发。因此,您将保留文本框颜色属性。
0赞 Schadensbegrenzer 8/22/2018
...或者您在 NumberCheck 函数中执行 A。return false

答:

0赞 usha 8/22/2018 #1
    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" />