$(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>
编辑:
引用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/