日期:2014-05-18  浏览次数:20400 次

这个JS该如何写呢?关于JS中用变量的问题
var   BackupStartTime   =   document.getElementById( " <%=txtbackupstarttime2.ClientID   %> ");
var   BackupStartTimeValue;
if   (inputList[0].checked   &&   inputList[0].value   ==   "Not   backup ")
{
      BackupStartTimeValue   =   BackupStartTime.value;
      BackupStartTime.value   =   " ";
}
else
{
      BackupStartTime.value   =   BackupStartTimeValue;
}
我上面的代码写的是有一个RadioButtonList,当我点它里面的某个单选框就把txtbackupstarttime2输入框中的值清空,当我点另一个就在还原回来。
但是象我这么写还象不行啊,else的时候得到的BackupStartTimeValue是未定义的,要说明的是:现在假设if   里的语句是肯定会执行的,就是说BackupStartTimeValue肯定会被赋值的。
怎么写让点else的时候会还原值?

------解决方案--------------------
var BackupStartTime = document.getElementById( " <%=txtbackupstarttime2.ClientID %> ");
var BackupStartTimeValue;

这两句放到函数外面

去掉var
BackupStartTime = document.getElementById( " <%=txtbackupstarttime2.ClientID %> ");
BackupStartTimeValue;

这样试试

------解决方案--------------------
<head runat= "server ">
<title> 无标题页 </title>
<script type= "text/javascript ">
var BackupStartTimeValue = " ";
function RadioClick(obj)
{
var BackupStartTime = document.getElementById( " <%=txtbackupstarttime2.ClientID %> ");
if (obj.value == "Not backup ")
{
BackupStartTimeValue = BackupStartTime.value;
BackupStartTime.value = " ";
}
else
{
BackupStartTime.value = BackupStartTimeValue;
}
}
</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<asp:RadioButtonList ID= "inputList " runat= "server " >
<asp:ListItem Value= "Not backup " > Not backup </asp:ListItem>
<asp:ListItem Value= "BBB "> bbbb </asp:ListItem>
<asp:ListItem Value= "CCC "> cccc </asp:ListItem>
<asp:ListItem Value= "DDD "> dddd </asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID= "txtbackupstarttime2 " runat= "server "> abcdefg </asp:TextBox>
</form>
</body>

****************************************************************
protected void Page_Load(object sender, EventArgs e)
{
foreach(ListItem item in this.inputList.Items)
item.Attributes.Add( "onclick ", "RadioClick(this); ");
}
------解决方案--------------------
假设if 里的语句是肯定会执行的,就是说BackupStartTimeValue肯定会被赋值的。
怎么写让点else的时候会还原值?
楼主的话有点不明白
都执行IF了怎么会执行ELSE里面的程序呢?