当出生日期填充MVC 3 Razor视图时自动显示年龄



在我的添加表单中,我有一个显示出生日期和年龄的文本框。我希望AGE的文本框将获得BIRTHDATE-DateTime.Now文本框中的值,并立即显示它。

我可以像compare属性一样在模型类中执行此操作吗。

您可以用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调用。

最新更新