最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
    }
});
发布评论

评论列表(0)

  1. 暂无评论