WEB/html,CSS

[html] Window.confirm 삭제하시겠습니까?

자바칩 프라푸치노 2021. 11. 27. 19:03

https://www.w3schools.com/jsref/met_win_confirm.asp

 

Window confirm() Method

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

뭔가를 삭제하기 전에 삭제 하시겠습니까? 해서 네 하면 삭제되고 아니오 하면 삭제 안되고 하는 것을 구현하고 싶었다.

이걸 커스텀 모달을 만들어서 구현해야하는지 고민했는데 Window.confirm 이라는 것을 알게되었다. 

 

var txt;
var r = confirm("Press a button!");
if (r == true) {
  txt = "You pressed OK!";
} else {
  txt = "You pressed Cancel!";
}

네를 누르면 true가 되고 아니면 false가 된다.

완전 편리하다

 

참고로 vue에서는 이렇게 구현했다.

confirm이 true일때 api. 요청을 하도록 했고 false일때는 아무일도 하지 않도록 했다. 

const deleteCollection = async () => {
      const confirm = window.confirm('정말 삭제하시겠습니까?');
      if (confirm) {
        const fetchData = await deleteMyCollectionAPI(props.collection.id);
        if (fetchData) {
          store.dispatch('user/deleteMadeCollection', props.collection.id);
        }
      }
    };
728x90