いつもお世話になっております。
フォーム内のボタンの遷移先を分岐させる必要が出てきました。
ラジオボタンの値を変更した時に、遷移先を変更させることは出来たのですが、戻るボタンで戻した時など、うまく動作しません。
例)
初期値(google)でExec→OK
Yahooに変更してExec→OK
戻るボタンで戻してExec→NG(Googleが開く)
Yahooに変更してExec→OK
戻した状態の時にもactionの値をキープする方法はないでしょうか?
html
1<html> 2<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 3<script type="text/javascript"> 4<!-- 5window.onpageshow = function(event) { 6 if (event.persisted) { 7 window.location.reload(); 8 } 9}; 10$(function () { 11 var val = $('input[name="typ"]').val(); 12 if(val == '1'){ 13 $(this).parents('form').attr('action', "https://www.yahoo.co.jp/"); 14 } else { 15 $(this).parents('form').attr('action', "https://www.google.com/"); 16 } 17 // ラジオボタンの選択先に応じてFormのAction先を変える 18 $('input[name="typ"]').change(function () { 19 var val = $(this).val(); 20 if(val == '1'){ 21 $(this).parents('form').attr('action', "https://www.yahoo.co.jp/"); 22 } else { 23 $(this).parents('form').attr('action', "https://www.google.com/"); 24 } 25 }); 26}); 27// --> 28</script> 29</head> 30<body> 31<form action="https://www.google.com" name=f1 method="GET"> 32<input type="radio" name="typ" value="1">Yahoo 33<input type="radio" name="typ" value="2" checked>Google<br> 34<input type='submit'>Exec 35</form> 36</body> 37</html>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。