简体   繁体   English

OnClientClick阻止OnClick事件?

[英]OnClientClick preventing OnClick event?

I have a button, I need to fire an onclick event, and an onclientclick event. 我有一个按钮,我需要触发一个onclick事件和一个onclientclick事件。 The onclientclick would be something like this: onclientclick将是这样的:

(function($){
$.fn.validate = function()  {
    var option = $('#<%=LblWarnings.ClientID%>').html();   
        if (option == "option1_OK" ||
            option == "option1_OK")  
            alert(wrong);
        else {
            $('#<%=LblWarnings.ClientID%>').html("Incomplete");
            alert(wrong);}        
    };
})(jQuery);

Validating function, checks the text of a label, if condition's true it should go on and execute the OnClick event. 验证函数,检查标签的文本,如果条件为真,它应该继续并执行OnClick事件。 And if it's false, it should NOT FIRE the code behind (method called WriteInfo in my .cs file, but just update a label so the user knows something was wrong while he was submitting data. 如果它是假的,它不应该消除后面的代码(我的.cs文件中称为WriteInfo的方法,但只是更新标签,以便用户在提交数据时知道出错了。

Is it possible for the onclientclick to stop the onclick event? onclientclick是否可以停止onclick事件? If so, could you help me providing me the asp:button sentence so I can trigger both events. 如果是这样,你能帮我提供asp:button句子,这样我就能触发这两个事件。

Thanks. 谢谢。

After trying Adil Answer, I get an error "object expected" 在尝试Adil Answer后,我收到错误“对象预期”

Maybe there's something wrong the way I'm calling the function? 也许我调用函数的方式有问题?

<asp:Button ID="ButtonRadioValue" CssClass="customButton" runat="server" onclick="WriteInfo" OnClientClick="validate();" Text="Accept" />

You can use stopPropagation() method to stop postback of return false. 您可以使用stopPropagation()方法来停止返回false的回发。

Use stopPropagation(); 使用stopPropagation();

 (function($){
    $.fn.validate = function(event)  {
        var option = $('#<%=LblWarnings.ClientID%>').html();   
            if (option == "option1_OK" ||
                option == "option1_OK")  
                alert(wrong);
                event.stopPropagation();
            else {
                $('#<%=LblWarnings.ClientID%>').html("Incomplete");
                alert(wrong);}        
        };
    })(jQuery);

Using return false; 使用return false;

(function($){
$.fn.validate = function()  {
    var option = $('#<%=LblWarnings.ClientID%>').html();   
        if (option == "option1_OK" ||
            option == "option1_OK")  
            alert(wrong);
            return false;
        else {
            $('#<%=LblWarnings.ClientID%>').html("Incomplete");
            alert(wrong);}        
    };
})(jQuery);

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

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