2013-01-03 24 views
0

我有2個按鈕,我要做到以下幾點但目前還不做任何事情: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> 

UPDATE:

<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> 
+0

你得到的錯誤是什麼?檢查您的控制檯 – Ankush

回答

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頁面之後。

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



}); 

然後你得到持有t他按鈕並準備事件處理程序。

$('.button').click(function(){ 
    enableHandler(); 
}); 
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; 
    } 
}); 
+0

我假設現在更新中的新代碼是正確的? – user1941871

+0

我仍然看到一個問題 - 查看Palash的代碼。你需要在java中使用'then',因爲ajax是非同步的 – Hogan

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()); 

});