请教一个输入数字后判断的问题
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>无标题页</title> <script src="../../js/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ var txtOne=document.getElementById("txtRedOne"); if(txtOne<10) { txtOne="0"+txtOne; } } </script></head><body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtRedOne" runat="server" Text="" onkeyup="if(isNaN(value))execCommand('undo')" MaxLength="12" BorderStyle="None"></asp:TextBox> </div> </form></body></html>问题:当我输入数字小于10的时候,前面加0,例如,我输入数字5,当鼠标移出,就会变为05!
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <div> </div> </form></body><script> document.getElementById("TextBox1").onblur = function () { if (this.value.length < 2) this.value = "0" + this.value; }</script></html>
[解决办法]
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>无标题页</title><script src="../../js/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $('#txtRedOne').blur(function() { var val = parseInt($(this).val(),10); if (val < 10) $(this).val('0' + val); else $(this).val(val); }); }); </script></head><body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtRedOne" runat="server" Text="" onkeyup="if(isNaN(value))execCommand('undo')" MaxLength="12" BorderStyle="None"></asp:TextBox> </div> </form></body>