我有2个按钮,我想做以下操作,但目前没有做任何事情:
启用按钮:
enableHandler()
功能,如果是用户
确认确认,然后导航用户到
penaltymarks.php
页。禁用按钮:
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>
答案 0 :(得分: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();
});
答案 1 :(得分: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;
}
});
答案 2 :(得分: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
页面之后。
答案 3 :(得分: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());
});