0

I am trying to block submit only in case a condition is met. If the condition matches it blocks the submit and works fine. But in case the condition not met, it still blocks the submit. Strange part is, it works fine when button is clicked again. I tried explicit postback as well, but no luck. I am sure I am missing something minor, but can't figure out.

My code looks like below.

 $('#GetAvailability').click(function (e) {
                 

 var valRetPax = $("#hdnRetPax").val();

 var valSelPax = $("#ddlPaxNum").val();

 var existed = valRetPax != '0' && valRetPax != valSelPax;

 alert(existed);
 var btnBack = $(this);

 if(existed)
    e.preventDefault();
 else
    __doPostBack($(btnBack).attr('name'), '');

     // Some other code after preventing the click....
});

and the button is defined as below.

<asp:Button Text="GO" runat="server" ID="GetAvailability" CssClass="btn btn-primary " OnClick="GetAvailability_Click" ClientIDMode="Static" />

Please help

1 Answer 1

1

Why not simply return false in the click function. That is all you need.

$('#GetAvailability').click(function (e) {

    if (existed)
        return false;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks... I have some other logic below after preventing the event. Updated the question to show the same.
Btw, I tried this too after my logic steps. Still the same issue. It works fine when condition is true. It blocks and my code gets executed etc. When its false, it doesnt work for the first time. It works when I click second time !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.