admin管理员组文章数量:1406347
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
4 Answers
Reset to default 2use 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
}
});
版权声明:本文标题:How to implement a confirm dialog box with If condition in JQueryJavascript with Asp.net - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745002515a2637065.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论