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

2

您根本没有将它们声明为函数,首先您必须这样做

$(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;
  }
};



});

然后你抓住按钮并准备事件处理程序。

$('.button').click(function(){
    enableHandler();
});
于 2013-01-03T13:11:26.470 回答
1

您不使用 ajax 功能导航到新页面,window.location而是使用。

window.location = "penaltymarks.php",

您还需要将代码挂钩以形成,使用 jQuery 您可以执行以下操作:

 $('#buttonID').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;
    }
 });
于 2013-01-03T13:12:55.193 回答
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();
    });

});​

disableHandler我使用的函数中$.when。这在这里用于等待 ajax 调用完成并在其完成跳转到completes.php页面之后。

于 2013-01-03T13:27:49.200 回答
0

而是调用两个未命名的函数来在其中声明您的函数,请尝试使用以下语法:

$(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 回答