如何在 ASP .NET MVC 5 视图中重命名 confirm() 按钮?

How Rename confirm() buttons in ASP .NET MVC 5 View?

我在view里面写脚本标签function.When我在view里点击菜单项调用这个函数

我想重命名 Java 脚本 confirm() 按钮。我怎样才能做到这一点 ?我不知道如何为这种情况编写 Java 脚本代码。

我按照下面的代码

<script type="text/javascript">
function myFunction() {
    var x;
    if (confirm("Press a button") == true) {
        x = "You pressed Cancel";
    } else {
        x = "You pressed OK";
    }
}
</script>
<li><a><span class="glyphicon glyphicon-log-in" onclick="myFunction()"> </span> Exit</a></li>

回答

您无法控制 confirm() 或 alert() Javascript 对话框的外观,因为它们是原生的,因此它们的设计基于 OS/browser.

你能做什么:

  • 自己的对话
  • 扩展/插件
  • 使用现有对话框 js

构建自己的点击元素的方法

document.querySelector('#okLI').addEventListener('click', handleClick);
document.querySelector('#cancelLI').addEventListener('click', handleClick);

function handleClick(e){
  var target = e.target;
  var clickType = target.getAttribute("click-type");
  if(clickType > 0)
    console.log("CLICKED OK");
  if(clickType < 0)
    console.log("CLICKED CANCEL");
}

document.querySelector('#ok').addEventListener('click', clickButton);
document.querySelector('#cancel').addEventListener('click', clickButton);

function clickButton(e){
  var button = e.target;
  var buttonType = button.getAttribute("button-type");
  if(buttonType > 0)
    console.log("CLICKED OK");
  if(buttonType < 0)
    console.log("CLICKED CANCEL");
}
<ul>
  <li id="okLI" click-type="1">OK</li>
  <li id="cancelLI" click-type="-1">CANCEL</li>
</ul>
<button id="ok" button-type="1">OK</button>
<button id="cancel" button-type="-1">CANCEL</button>

您不需要使用按钮,您可以将它添加到您想要单击的每个元素。

根据您的要求,请检查此功能的 jQuery 模态对话框。

您需要在页面中包含以下 css 和 JS。

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">    
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

然后在您的页面上添加以下脚本。 以下脚本将为您配置一个带有两个按钮的对话框,"Delete" 和 "Cancel",如您所见,它们都有一个回调函数,您可以在其中决定单击时要执行的操作。

<script type="text/javascript">
  $( function() {
    $( "#dialog-confirm" ).dialog({
      resizable: false,
      height: "auto",
      width: 400,
      modal: true,
      buttons: {
        "Delete": function() {
         // YOUR LOGIC GOES HERE
          $( this ).dialog("close");
        },
        Cancel: function() {
          $(this).dialog( "close" );
        }
      }
    });
  } );
  </script>

然后在您的页面上添加以下内容HTML

<div id="dialog-confirm" title="Delete Item?">
  <p>Do you want to proceed?</p>
</div>

最后你只需要调用下面的函数,当你想显示对话框。 例如,要在单击按钮时触发它..你可以像下面这样

<button type="button" id="btn_delete">Delete</button>

<script type="text/javascrpt">
$("#btn_delete").click(function(){
 $( "#dialog-confirm").dialog( "open" );
});
</script>

您可以在以下 link.

中找到更多详细信息

jQuery Dialog

为javascript确认 您不能重命名 Javascript Confirm 的按钮标题,因为它是由浏览器管理的,没有任何库或插件可以操纵这些组件。