LoginSignup
15
19

More than 5 years have passed since last update.

JSのconfirmでダイアログのボタン押下時の処理の分岐

Last updated at Posted at 2018-11-21

HTML側で以下のボタンを押下した場合にOKorキャンセルのwindowを出したい。

hoge.html
<form method="POST" id="id" action="/users/delete/">
    <input type="submit" value="削除">
</form>

jsのconfirmメソッドを使用してみる。

hoge.html

//jsの処理
<script type="text/javascript">
  function Check(){
      var checked = confirm("削除します");
      if (checked == true) {
          return true;
      } else {
          return false;
      }
  }
</script>

//inputタグにonClick追記
<form method="POST" id="id" action="/users/delete/">
    <input type="submit" value="削除" onClick="return Check()">
</form>

因みにonClickにreturnを記述しないと、
キャンセル押下時もactionの処理に遷移するので注意する。

15
19
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
19