如何创建带有“确定"和“取消"选项的对话框

2022-01-15 00:00:00 dialog javascript

我将制作一个按钮来执行操作并将数据保存到数据库中.

I am going to make a button to take an action and save the data into a database.

一旦用户单击按钮,我希望 JavaScript 警报提供是"和取消"选项.如果用户选择是",则将数据插入数据库,否则不执行任何操作.

Once the user clicks on the button, I want a JavaScript alert to offer "yes" and "cancel" options. If the user selects "yes", the data will be inserted into the database, otherwise no action will be taken.

如何显示这样的对话框?

How do I display such a dialog?

推荐答案

您可能正在寻找 confirm(),它会显示一个提示并返回 truefalse用户决定:

You’re probably looking for confirm(), which displays a prompt and returns true or false based on what the user decided:

if (confirm('Are you sure you want to save this thing into the database?')) {
  // Save it!
  console.log('Thing was saved to the database.');
} else {
  // Do nothing!
  console.log('Thing was not saved to the database.');
}

相关文章