简体   繁体   English

从HTML中的if语句调用Javascript函数

[英]Calling Javascript function from if statment in HTML

This code is wriiten in VS 2003 -asp.net 此代码在VS 2003 -asp.net中完成

in the HTML page, I have a javascript function checkuser() defined which returns boolean. 在HTML页面中,我有一个定义了javascript函数checkuser() ,它返回boolean。 I would like to enclose the following in a call to this function 我想在调用此函数时附上以下内容

<A onclick="stageClear(); stageEditor();" href="javascript: void(0);">Add new Stage</A>

I want something like 我想要类似的东西

<%if checkuser() then%> <A onclick="stageClear(); stageEditor();" href="javascript: void(0);">Add new Stage</A> <% end if %>

But I am getting the error checkuser() not defined 但我收到错误checkuser()未定义

Your code looks as if it is looking for an asp function called checkuser. 您的代码看起来好像在寻找名为checkuser的asp函数。

if you wanted to do it with js you would need to do something like this: 如果你想用js做,你需要做这样的事情:

<script type="text/javascript">
   if (checkuser()) {
      document.write('<a onclick="stageClear(); stageEditor();" href="javascript: void(0);">Add new Stage</a>');
   }
</script>

If you intend to call the checkuser() function inside a server-side code block (as the one you show), you must define the function on the page's code behind, not on the client-side using JavaScript. 如果您打算在服务器端代码块(如您所示的代码块checkuser()内调用checkuser()函数,则必须在页面代码后面定义函数,而不是在客户端使用JavaScript定义。

In other words, you need to have a function called 换句话说,您需要有一个名为的函数

protected bool checkuser()
{
    return true;//just an example
}

And then you can call it as you intended to. 然后你可以按照你的意愿调用它。

You can't mix your back end and your front end code. 您无法混合后端和前端代码。

<a onclick="clickHandler()" href="javascript:void(0)">Stuff</a>

and the JS: 和JS:

<script>
    function clickHandler(){
        if(checkuser()){
            //do something
        }else{
            //do something else
        }
    }
</script>

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

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