osh*_*nen 25 vb.net asp.net asp.net-3.5 onclientclick
是否可以使用onclientclick按钮的属性进行客户端检查.如果检查返回true,则触发该onclick事件.如果客户端检查返回false,请不要触发该onclick事件.
那可能吗?
更新:
这2项工作:
Stops the form from submitting:
OnClientClick="return false;"
Run Code Online (Sandbox Code Playgroud)
Allows the form to submit:
OnClientClick="return true;"
Run Code Online (Sandbox Code Playgroud)
接下来的2个不起作用:
// in js script tag
function mycheck() {
return false;
}
// in asp:button tag
OnClientClick="return mycheck();"
Run Code Online (Sandbox Code Playgroud)
// in js script tag
function mycheck() {
return true;
}
// in asp:button tag
OnClientClick="return mycheck();"
Run Code Online (Sandbox Code Playgroud)
它同时提交表单.
这是为什么?
Win*_*Win 42
您想return在调用函数后添加OnClientClick内部.否则,即使函数返回false,该按钮也会回发.
<asp:button ID="Button1" runat="server" OnClick="Button1_Click"
OnClientClick="return checkValidation()" Text="Submit" />
<script type="text/javascript">
function checkValidation() {
return confirm('Everything ok?');
}
</script>
Run Code Online (Sandbox Code Playgroud)
当然.如果您return false在OnClientClick其中使用它将阻止任何导航发生.所以你的代码看起来像:
<asp:LinkButton runat="server" OnClientClick="if(!ValidatePage()) { return false;}" />
Run Code Online (Sandbox Code Playgroud)