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

Milind Thakkar
  • 980
  • 2
  • 13
  • 20

1 Answers1

1

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

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

    if (existed)
        return false;
});
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Thanks... I have some other logic below after preventing the event. Updated the question to show the same. – Milind Thakkar Jul 05 '20 at 12:55
  • 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 ! – Milind Thakkar Jul 05 '20 at 13:06