Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 简单Javascript函数-2个选项,每个选项指向不同的URL_Php_Javascript - Fatal编程技术网

Php 简单Javascript函数-2个选项,每个选项指向不同的URL

Php 简单Javascript函数-2个选项,每个选项指向不同的URL,php,javascript,Php,Javascript,我想知道当有人点击链接时,如何弹出确认/拒绝框。当他们点击“确定”时,它会把他们带到一个地方,当他们点击“取消”时,它会把他们带到另一个地方。我的大部分经验是使用PHP,但通常我可以使用其他JavaScript函数并使它们工作。。。但我似乎还没弄明白这一点 这是我的剧本: function confirmChoice(name, newid){ answer = confirm("Transfer Ownership of this task to " + name +

我想知道当有人点击链接时,如何弹出确认/拒绝框。当他们点击“确定”时,它会把他们带到一个地方,当他们点击“取消”时,它会把他们带到另一个地方。我的大部分经验是使用PHP,但通常我可以使用其他JavaScript函数并使它们工作。。。但我似乎还没弄明白这一点

这是我的剧本:

function confirmChoice(name, newid){
    answer = confirm("Transfer Ownership of this task to " + name + 
                     "? Press Cancel to decline.")
    if (answer == 1) {
        location = "transfer.php?task=<? echo $taskid; ?>&from=<? echo ownerid; ?>&to=" + newid;
    } elseif (answer==0) {
        location = "decline.php?task=<? echo $taskid; ?>";
    }   
}
函数confirmChoice(名称,newid){
回答=确认(“将此任务的所有权转移到”+名称+
“?按“取消”以拒绝。”)
如果(答案=1){
location=“transfer.php?task=&from=&to=“+newid;
}elseif(答案==0){
location=“declient.php?task=”;
}   
}
任何帮助都将不胜感激

编辑:好的,按照建议更改代码。现在是:

function confirmChoice(name, newid){
var answer = confirm("Transfer Ownership of this task to " + name + "? Press Cancel to decline.")
if (answer){
location = "transfer.php?task=<? echo $thistaskid; ?>&from=<? echo $ownerid; ?>&to=" + newid;
}else{
location="decline.php?task=<? echo $thistaskid; ?>";
}   
}
函数confirmChoice(名称,newid){
var answer=confirm(“将此任务的所有权转移到“+name+”?按“取消”以拒绝”)
若有(答复){
location=“transfer.php?task=&from=&to=“+newid;
}否则{
location=“declient.php?task=”;
}   
}
使用的链接是:

<a href="#" onclick="confirmChoice(<? echo $requestorname; ?>, <? echo $newid; ?>); return false;"><? echo $requestorname; ?> Requested Ownership</a>


我仍然没有得到一个确认框…

让它成为
top.location
而不仅仅是
location

此外,我建议不要使用
answer==1
进行比较

confirm
返回一个布尔值。如果(回答){

避免使用
answer==1
的原因是,执行
==1
是一种不严格的比较,如果您不知道JavaScript的行为,它最终会咬到您。
answer==true
也是一种可以接受的方法

另一个重要的细节是,执行此操作时:

answer = confirm("something here");
您正在将answer声明为全局变量。这是一种糟糕的做法,您应该避免它。只需在它之前添加
var
,即可修复它

var answer = confirm("whatever");

有关javascript比较的详细信息:

如果您使用的是window.open(),则可以轻松更改目标位置,但需要检查确认返回的内容

var res = confirm("Do you agree?");
并据此确定交通方向

var options = ['http://google.com', 'http://boston.com'];
window.location = options[ res ? 1 : 0 ];
// or
window.open = options[ res ? 1 : 0 ];
小提琴不想演奏得很好,但它在我的本地机器上对我很有用。

嗯,谢谢,但这仍然不起作用。我没有收到确认框。很可能你的函数从未被调用。在我提供帮助之前,你必须提供其他代码。谢谢你的帮助!我做了你建议的更改。页面上的链接只是为了确定。你是这样做的吗?onclick=“confirmChoice('some name',123);return false;”因为名称的单引号很重要,所以作为额外的引号,您可能希望转义$requestorname以避免XSS漏洞。请使用htmlentities。