Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从OnClientClick调用函数时在JavaScript中返回true_Javascript_Asp.net - Fatal编程技术网

从OnClientClick调用函数时在JavaScript中返回true

从OnClientClick调用函数时在JavaScript中返回true,javascript,asp.net,Javascript,Asp.net,当我使用此代码时: <asp:Button runat="server" ID="Block" Text="text" OnClientClick="return BlockCheck()" OnClick="BtnBlockClick" CausesValidation="False"/> function BlockCheck() { if (x) { document.getElementById('#Erro

当我使用此代码时:

<asp:Button runat="server" ID="Block" Text="text"  OnClientClick="return BlockCheck()"   OnClick="BtnBlockClick"  CausesValidation="False"/>


function BlockCheck() {
            if (x) {
                document.getElementById('#ErrorText').text('Error');
                return false;
            }
        return true;
    }

函数块检查(){
if(x){
document.getElementById('#ErrorText').text('Error');
返回false;
}
返回true;
}
但是这一部分的代码是:
document.getElementById('ErrorText').text('Error')

返回true。

首先,在getElementById的参数中不需要
:它确实应该这样写

document.getElementById('ErrorText')
使用
#
将尝试查找ID实际上等于
'#ErrorText'
的元素-这将失败,因此函数将返回
null
。之后任何使用
null
的尝试(特别是取消某些方法)都将失败并引发错误

其次,您似乎混合了jQuery函数和JavaScript函数:doElement中没有
text
方法。严格地说,你必须检查
textContent
支持,然后再去做;如果不支持,请改用
innerText
。例如:

var el = document.getElementById('ErrorText');
el['textContent' in el ? 'textContent' : 'innerText'] = 'Error';

显然,如果您的项目不需要支持IE8-。

什么是
x
?定义在哪里?x是真的条件