LoginSignup
1
1

More than 5 years have passed since last update.

htmlのページャー番号をjsとフォームに渡して実行する方法

Posted at

ページャーのリンクをクリックしたときにjsのfunctionを呼び出して
クリックしたページ番号をフォームの要素に追加し、フォームをsubmitする方法。

<a href="#" onclick="funcPage(ページ番号);">ページ番号</a>'
    <script language="javascript" type="text/javascript">
    function funcPage(ページ番号) {
        // エレメントを作成
        var ele = document.createElement('input');
        // データを設定
        ele.setAttribute('type', 'hidden');
        ele.setAttribute('name', 'page');
        ele.setAttribute('value', page);
        // 要素を追加
        document.forms[0].appendChild(ele);
        document.forms[0].submit();
    }
    </script>

ちなみにphpで受け取ったページ番号が空だったら1を入れる書き方。

$page = empty($_POST["page"])? 1:$_POST["page"];
1
1
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
1
1