いつもお世話になっております。
フォーム内のボタンの遷移先を分岐させる必要が出てきました。
ラジオボタンの値を変更した時に、遷移先を変更させることは出来たのですが、戻るボタンで戻した時など、うまく動作しません。
例)
初期値(google)でExec→OK
Yahooに変更してExec→OK
戻るボタンで戻してExec→NG(Googleが開く)
Yahooに変更してExec→OK
戻した状態の時にもactionの値をキープする方法はないでしょうか?
html
<html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> <!-- window.onpageshow = function(event) { if (event.persisted) { window.location.reload(); } }; $(function () { var val = $('input[name="typ"]').val(); if(val == '1'){ $(this).parents('form').attr('action', "https://www.yahoo.co.jp/"); } else { $(this).parents('form').attr('action', "https://www.google.com/"); } // ラジオボタンの選択先に応じてFormのAction先を変える $('input[name="typ"]').change(function () { var val = $(this).val(); if(val == '1'){ $(this).parents('form').attr('action', "https://www.yahoo.co.jp/"); } else { $(this).parents('form').attr('action', "https://www.google.com/"); } }); }); // --> </script> </head> <body> <form action="https://www.google.com" name=f1 method="GET"> <input type="radio" name="typ" value="1">Yahoo <input type="radio" name="typ" value="2" checked>Google<br> <input type='submit'>Exec </form> </body> </html>
まだ回答がついていません
会員登録して回答してみよう