How to implement a confirm dialog box with If condition in JQueryJavascript with Asp.net - Stack Overflow

I have a bit of Jquery (sort of mixed with traditional javascript) to check a textbox. On my sample asp

I have a bit of Jquery (sort of mixed with traditional javascript) to check a textbox. On my sample asp page, there is a button, a textbox and on code behind there is a button_click event. What I am trying to get is, when someone clicked on the button, if the textbox is empty, they will see a alert box. When there is a value, they will see a confirm dialog box. My code does most of the job but when the user see confirm dialog box, and cancel, the server side code got executed.

What am I missing here?

Thanks in advance.

$(document).ready(function () {
    $("#Button1").click(function (e) {

        var a = $("#TextBox1").val();

        if (jQuery.trim(a).length > 0) {
            confirm('To confirm  click OK ');
        }
        else {
            alert("Empty");
            e.preventDefault(); 
        }
    });
}); 

I have a bit of Jquery (sort of mixed with traditional javascript) to check a textbox. On my sample asp page, there is a button, a textbox and on code behind there is a button_click event. What I am trying to get is, when someone clicked on the button, if the textbox is empty, they will see a alert box. When there is a value, they will see a confirm dialog box. My code does most of the job but when the user see confirm dialog box, and cancel, the server side code got executed.

What am I missing here?

Thanks in advance.

$(document).ready(function () {
    $("#Button1").click(function (e) {

        var a = $("#TextBox1").val();

        if (jQuery.trim(a).length > 0) {
            confirm('To confirm  click OK ');
        }
        else {
            alert("Empty");
            e.preventDefault(); 
        }
    });
}); 
Share Improve this question asked Oct 22, 2012 at 14:41 LaurenceLaurence 7,83322 gold badges83 silver badges129 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

use function below for button's OnClientClick property:

 function checkClick() {
      var result = $.trim($("#<%= TextBox1.ClientID %>").val()).length > 0;
      if (result === true) {
           result = confirm("To confirm  click OK");
      }
      else {
           alert("oops!");
      }
      return result;
 }

 <asp:Button runat="server" Text="Click Me" OnClientClick="return checkClick()" />

Confirm : result is a boolean value indicating whether OK or Cancel was selected (true means OK).

           if (!confirm('To confirm  click OK ')) return false; //don't do anything

You need to prevent the default behavior (which I assume is a a submit) if they press cancel on the confirm. confirm returns a boolean which will allow you to do this.

if (jQuery.trim(a).length > 0) {
    var answer = confirm('To confirm  click OK ');

    if (!answer) {
        e.preventDefault(); 
    }
}

Use function below for button or a href with class defined when onclick

$("body").on("click",".yourclassname",function(){
    var delet = confirm('Are you sure want to delete?');
    if(delet){
        // Some function when true
    }else{
        // Some function when false
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745002515a4605583.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信