当 onclientclick 为假时停止 onclick 触发? [英] Stopping onclick from firing when onclientclick is false?

查看:36
本文介绍了当 onclientclick 为假时停止 onclick 触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用按钮的 onclientclick 属性进行客户端检查.如果检查返回 true,则触发 onclick 事件.如果客户端检查返回 false,则不要触发 onclick 事件.

Is it possible to use the onclientclick property of a button to do a clientside check. If the check returns true, then fire the onclick event. If the clientside check returns false, don't fire the onclick event.

这可能吗?

更新:

这两个工作:

Stops the form from submitting:
OnClientClick="return false;"

<小时>

Allows the form to submit:
OnClientClick="return true;"

<小时>

接下来的两个不起作用:


The next 2 do not work:

// in js script tag
function mycheck() {
    return false;
}

// in asp:button tag
OnClientClick="return mycheck();"

<小时>

// in js script tag
function mycheck() {
    return true;
}

// in asp:button tag
OnClientClick="return mycheck();"

它两次提交表单.

这是为什么?

推荐答案

您想在调用函数后在 OnClientClick 中添加 return .否则,即使函数返回 false,按钮也会回发.

You want to add return inside OnClientClick after a function is called. Otherwise, the button will post back even if function returns 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>

这篇关于当 onclientclick 为假时停止 onclick 触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆