开发者

onclientClick imageButton return false doesn't work + Answer

I've search about 30min about that.

Calling this didn't work. An alert (a()) would return false but postback anyway.

function a()
{
   var a = new Boolean();
   return a;
}

But that worked, didn't postback:

function a()
{
   var开发者_StackOverflow社区 a = new Boolean();
   a = false;
   return a;
}


How are you placing the OnClientClick? A Javascript return false and an .NET return false are not the same. If your code is

<asp:ImageButton runat="server" OnClientClick="a()" />

then only javascript returns false. let .NET know the response using return:

<asp:ImageButton runat="server" OnClientClick="return a()" />


Most likely, the code that is consuming your function is expecting false not "falsey". If the object new Boolean() is coerced to a boolean value, it will return false. But, the object new Boolean() is not the same as the value false.

new Boolean() == false; // this is true
new Boolean() === false; // this is false!

Most likely, the consuming code is doing an === comparison, not an == comparison. Just do:

function a()
{
    var b = false;
    return b;
}


Boolean is an object, you have to return

function a()
{
   var a = new Boolean();
   return a.valueOf();
}


If a method doesn't return anything, it technically returns "falsey", so to avoid ambiguity browsers require "real false" to be returned. So in your case you can do this:

function a()
{
   var a = new Boolean();
   return !!a;
}

!! will cast your value to real boolean


in this function

function a()
{
   var a = new Boolean();
   return a;
}

You are declaring var a = new Boolean(); and returning the Boolean object NOT VALUE
You type cast it as Boolean but did not set a value which means it is undefined.


in your function

function a()
{
   var a = new Boolean();
   a = false;
   return a;
}

You are assigning a as a Boolean object and then reassigning it as a Boolean value of false and returning the Boolean variable false;


Also you can do this.

function a()
{
   return false;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜