简体   繁体   English

按钮上的JavaScript函数单击

[英]JavaScript Function on Button Click

Hi I am working on java script,i am writing java script function on aspx page and calling that function,the function will return value on button click event,But when i click on button the function is not getting called Here is my code. 嗨,我正在研究Java脚本,我正在ASPP页面上编写Java脚本函数并调用该函数,该函数将在按钮单击事件时返回值,但是当我单击按钮时,该函数未得到调用这是我的代码。

Java Script Function Java脚本功能

function CheckAge(age)
{
if (age > 18)
return true;
else
return false;                
}

Here is on Button Click Event in CSfile 这是CSfile中的按钮单击事件

try
{
bool check = false;
try
{
btnAge.Attributes.Add("onClick", "javascript:return CheckAge('" + txtAge.Text + "')");
if (check)
Response.Write("You Are Eligible");
else
Response.Write("You Are Not Eligible");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
catch (Exception)
{
throw;
} 
}

Please help me out,can give the code please 请帮帮我,请给我密码

the variable check is not set. 未设置变量check try this: 尝试这个:

  btnAge.Attributes.Add("onClick", "javascript:var check=CheckAge('" + txtAge.Text + "')");

The problem is the .aspx code is computed before the page is sent to the user. 问题是.aspx代码是在将页面发送给用户之前计算的。 The code will all be ran at once before the user ever receives the page. 该代码将在用户收到页面之前立即全部运行。 Once the user has the page, no amount of javascript could run aspx unless it refreshes the page or calls another one. 一旦用户拥有页面,除非刷新页面或调用另一个页面,否则没有大量的JavaScript可以运行aspx。

Either make the entire function in Javascript, or make it reload the page with a GET variable that the aspx can process. 请使用Javascript编写整个函数,或使用aspx可以处理的GET变量重新加载页面。

JS: JS:

function CheckAge(age)
{
    if (age > 18) document.location.href = "index.aspx?allowed=1";
    else document.location.href = "index.aspx?allowed=0";                
}

ASP: ASP:

try
{
    if(Request.QueryString["allowed"]==1) Response.Write("You Are Eligible");
    if(Request.QueryString["allowed"]==0) Response.Write("You Are Not Eligible");
}
catch (Exception)
{
    throw;
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM