118
112

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

フォームのボタンでaction先を変更するときのベストプラクティス

Last updated at Posted at 2015-07-25

この記事は移行しました!最新の内容はこちらをご覧ください😀

毎回どう実装しようか迷うので
これからは下記で統一しようと思います。
※jQueryを使用

jsは汎用的に。

javascript
$('.submit').click(function() {
  $(this).parents('form').attr('action', $(this).data('action'));
  $(this).parents('form').submit();
});

HTMLはこんな感じ。

html
<form action="regist.php" method="POST">
  <button class="submit" data-action="regist.php">登録する</button>
  <button class="submit" data-action="input.php">入力画面に戻る</button>
</form>
  • data-actionに遷移先URLを設定
  • class="submit"を付与することでjsが起動します

※formのactionはjsで書き換えられるので何でもOKかと。

ステキです。

118
112
0

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
118
112

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?