How to create a dialog with “yes” and “no” options

2019-01-02 21:31发布

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

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?

11条回答
贼婆χ
2楼-- · 2019-01-02 22:19
document.getElementById("button").addEventListener("click", function(e) {
   var cevap = window.confirm("Satın almak istediğinizden emin misiniz?");
   if (cevap) {
     location.href='Http://www.evdenevenakliyat.net.tr';       
   }
});
查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-02 22:24

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!
} else {
    // Do nothing!
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-02 22:24

How to do this using 'inline' JavaScript:

<form action="http://www.google.com/search">
  <input type="text" name="q" />
  <input type="submit" value="Go"
    onclick="return confirm('Are you sure you want to search Google?')"
  />
</form>
查看更多
手持菜刀,她持情操
5楼-- · 2019-01-02 22:30

You can intercept the onSubmit event ising JS. Then call a confirmation alert and then grab the result.

查看更多
老娘就宠你
6楼-- · 2019-01-02 22:32

Or simply:

<a href="https://some-link.com/" onclick="return confirm('Are you agree to go to that link?');">click me!</a>
查看更多
登录 后发表回答