2011-01-20 108 views
0

我不确定这是更多的CodeIgniter问题或Javascript问题。Javascript确认和CodeIgniter

这里是我的javascript:

<script type="text/javascript"> 
function con(message) { 
var answer = confirm(message); 
if (answer) { 
    return true; 
} 

return false; 
} 
</script> 

这里是我的CI:

echo form_submit('submit', 'Login', 'onclick="return con("Are you sure you don\'t have a configuration to submit? Please check the checkbox if you do.")'); 

为什么我没有得到一个确认对话框? JS在我看来。 CI正确呈现提交按钮。

回答

4

您需要在JS使用单引号:

    // this double quote is causing issues with the onclick assignment. 
onclick="return con("Are you sure you don\'t have a configuration to submit? Please check the checkbox if you do.")' 

你甚至可以看到颜色从语法突出问题,使

我建议:

onclick="return con(\'Are you sure you don\\'t have a configuration to submit? Please check the checkbox if you do.\')"' 
0

是不是你的代码进入无限循环?

您已经重新定义了confirm() JS函数,并且您正在循环执行您。

+0

我改名为“骗子”,在这两个地方,我仍然没有得到对话框。 – sehummel 2011-01-20 16:51:45

1

此代码将进入无限循环,因为您将函数confirm命名为与构建在JavaScript confirm中相同。

因此,在您的confirm函数中,当它调用confirm时,它将调用您的函数,而不是本机函数。

原生JavaScript confirm函数返回true或false,所以不需要您的方法。

编辑:问题是当你调用form_submit。

变化:

'onclick="return con("Are you sure you don\'t have a configuration to submit? Please check the checkbox if you do.")' 

要:

'onclick="return con(\'Are you sure you don\\\'t have a configuration to submit? Please check the checkbox if you do.\')"' 
+0

这不是问题所在。我重命名了这个函数,但它仍然不起作用。 – sehummel 2011-01-20 16:53:17

+0

新的解决方案没有解决问题。我复制了你的代码,我仍然没有对话框。 – sehummel 2011-01-20 17:09:47

+0

@ shummel7845:问题是你需要确保你的引号被正确地转义,我需要确保我知道有多少''要投入。 – 2011-01-20 17:12:23