0

私は次のことをしたい2つのボタンを持っていますが、現時点では何もしていません:

有効化ボタン:

  • ユーザーが確認を確認すると、ボタン onclick はenableHandler()関数にアクセスし、ユーザーを penaltymarks.phpページに移動します。

無効化ボタン:

  • ボタン onclick はdisableHandler()関数にアクセスし、ユーザーが確認を確認すると、ユーザーを completes.phpページに移動し、ajax は completesession.phpバックグラウンドでページに移動します。

現時点でコードでは何も起こっていないため、ボタンで上記を実行するにはどうすればよいですか。<form>コードにフォームタグを含めていないので、タグが必要ですか。

<script type="text/javascript">

$(function() {   

    enableHandler() {
       if (confirm("Are you sure you wish to enable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) 
       {
         $.ajax({
           url: "penaltymarks.php",
           async: false,
           type: "POST"
         });
         return true;
      }
    };

});

$(function() {   

    disableHandler() {
       if (confirm("Are you sure you wish to disable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) 
       {
         $.ajax({
           url: "sessioncomplete.php",
           async: false,
           type: "POST"
         });
         return true;
      }
    };

});

</script>

アップデート:

<table>
<tr>
<td><input type="button" id="enablePenalty" value="Enable Penalty Marks"/></td>
<td><input type="button" id="disablePenalty" value="Do Not Enable Penalty Marks"/></td>
</tr>
</table>

<script type="text/javascript">

 $('#enablePenalty').click(function () {
    if (confirm("Are you sure you wish to enable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) 
    {
      window.location = "penaltymarks.php",
      return true;
    }
 });

  $('#disablePenalty').click(function () {
       if (confirm("Are you sure you wish to disable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) 
    {
       $.ajax({
           url: "sessioncomplete.php",
           async: false,
           type: "POST"
         });
      window.location = "complete.php",
      return true;
    }
 });


</script>
4

4 に答える 4

1

完全な作業コードは次のとおりです。

$(function() {

    function enableHandler() {
        if (confirm("Are you sure you wish to enable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) {
            window.location = "penaltymarks.php";
            return true;
        }
    }

    function disableHandler() {
        if (confirm("Are you sure you wish to disable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) {
            $.when($.ajax({
                url: "sessioncomplete.php",
                async: false,
                type: "POST"
            })).then(window.location = "completes.php");
            return true;
        }
    }

    // Enable Button
    $('#button1').click(function() {
        enableHandler();
    });

    // Disable Button
    $('#button2').click(function() {
        disableHandler();
    });

});​

関数では、$.whendisableHandlerを使用しました。これは、ajax 呼び出しが終了するのを待機し、ページへのジャンプが完了した後に使用されます。completes.php

于 2013-01-03T13:27:49.200 に答える
0

2 つの名前のない関数を呼び出して、その中で関数を宣言する代わりに、次の構文を使用してみてください。

$(document).ready( function(){



   function enableHandler() {
       if (confirm("Are you sure you wish to enable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) 
       {
         $.ajax({
           url: "penaltymarks.php",
           async: false,
           type: "POST"
         });
         return true;
       }
    };

  function disableHandler() {
   if (confirm("Are you sure you wish to disable Penalty Marks?" + "\n" + "(You cannot change your option once you have confirmed)" + "\n")) 
   {
     $.ajax({
       url: "sessioncomplete.php",
       async: false,
       type: "POST"
     });
     return true;
   }
  };

    //Here you can call functions
    $('#EnableButton').click( enableHandler());

});
于 2013-01-03T13:19:07.463 に答える