在我的添加表格中,我有一个BIRTHDATE和AGE的文本框。我希望AGE的文本框能够在BIRTHDATE - DateTime.Now的文本框中获取值并立即显示它。
我可以在我的模型类中执行此操作,例如compare属性。
答案 0 :(得分:0)
您可以在JavaScript或服务器端进行客户端计算,然后通过Ajax调用访问它。
这是我发现的一段JavaScript:
<script language="javascript">
<!--
function Age()
{
var bday=parseInt(document.forms[0].txtBday.value);
var bmo=(parseInt(document.forms[0].txtBmo.value)-1);
var byr=parseInt(document.forms[0].txtByr.value);
var byr;
var age;
var now = new Date();
tday=now.getDate();
tmo=(now.getMonth());
tyr=(now.getFullYear());
{
if((tmo > bmo)||(tmo==bmo & tday>=bday))
{age=byr}
else
{age=byr+1}
alert("As of today, "+now+' \n'+", you are:"+(tyr-age)+ " years old");
}}
//-->
</script>
如果您要使用Ajax解决方案,可以使用jQuery $.ajax
方法对MVC操作方法进行简单的Ajax调用。