c# - 在 javascript 中添加“确定”和“取消”按钮

标签 c# javascript jquery alert

   $(document).ready(function () {
        $('.abc').click(function () {
            var status = $("#<%=ddlstatus.ClientID%>").val;
            if (status == "Prepared") {
                var _Action = confirm('Do you really want to cancel this payment ? All pending money will be available in the retention account of the contractor ');
                if (_Action) {
                    $.blockUI({ css: {
                        border: 'none',
                        padding: '15px',
                        backgroundColor: '#000',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }
                    });
                    return true;
                }
                else {
                    return false;
                }


            }

        });
    }); 

当我运行此 JavaScript 时,我得到了“确定”按钮,但我还想添加一个取消按钮。另外,我从后面的 C# 代码中调用它

最佳答案

您可以尝试使用jQuery UI Dialog :

<div id="dialog" title="Confirmation Required">
  Do you really want to cancel this payment? All pending money will be available in the retention account of the contractor. 
</div>

<script type="text/javascript">
  $(document).ready(function() {
    $("#dialog").dialog({
      autoOpen: false,
      modal: true
    });
  });

  $(".abc").click(function(e) {
    e.preventDefault();
    var targetUrl = $(this).attr("href");
    var status = $("#<%=ddlstatus.ClientID%>").val();

    $("#dialog").dialog({
      buttons : {
        "Ok" : function() {              
         if (status == "Prepared") {
            $.blockUI({ css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#000',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                    }
                }); 
          }
          window.location.href = targetUrl;
        },
        "Cancel" : function() {
          $(this).dialog("close");
        }
      }
    });

    $("#dialog").dialog("open");
  });
</script>

演示:http://jsfiddle.net/rCVrc/

编辑:

引用Nick的回答here ,您可以使用ScriptManager.RegisterStartupScript()方法,像这样:

ScriptManager.RegisterStartupScript(this, GetType(), "modalscript",
    "$(function() { $('#dialog').dialog({
      buttons : {
        'Ok' : function() {              
         if (status == 'Prepared') {
            $.blockUI({ css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#000',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                    }
                }); 
          }
          window.location.href = targetUrl;
        },
        'Cancel' : function() {
          $(this).dialog('close');
        }
      }
    }); });", true);

“如果您不使用 ScriptManager/UpdatePanels,请使用等效的 ClientScriptManager 版本。

记住将代码包装在 document.ready 处理程序中(如果没有它,IE 会出现大多数问题),这一点很重要,因此您的元素(在我的示例中为 id="dialog")位于DOM 并准备就绪。”

关于c# - 在 javascript 中添加“确定”和“取消”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12919245/

相关文章:

C#对象和内存管理

javascript - 如何将背景图像从 React 中的状态映射到卡片上?

jquery-mobile - 如何使用 jQuery 函数实现 jQuery Mobile 页面滑动?

javascript - 如何使用 Valums Ajax File-Uploader 触发上传文件?

c# - DateTime.ToString 格式表达式是否受当前区域性影响?

c# - 关闭 Windows 8 Charm Bar

c# - 设置不可调整大小的 GridViewColumn

javascript - pug 迭代期间排序或随机播放列表?

JavaScript 函数阻止链接工作

javascript - 将脚本绑定(bind)到单选按钮: Weird Behaviour : Not Binding by Name