Javascript confirm and CodeIgniter
I'm not sure if this is more of a CodeIgniter question or a Javascript question.
Here is my Javasc开发者_Go百科ript:
<script type="text/javascript">
function con(message) {
var answer = confirm(message);
if (answer) {
return true;
}
return false;
}
</script>
Here is my 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.")');
Why am I not getting a confirmation dialog box? The JS is in my view. The CI renders the submit button correctly.
You need to use single quotes in your 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.")'
You can even see the issue with the color-syntax highlighting from SO
I recommend:
onclick="return con(\'Are you sure you don\\'t have a configuration to submit? Please check the checkbox if you do.\')"'
This code is going to go into an infinite loop because you named your function confirm
the same as the built in JavaScript confirm
.
So, in your confirm
function, when it calls confirm
it will be calling your function, not the native function.
The native JavaScript confirm
function returns true or false, so there is no need for your method.
EDIT: The issue is when you are calling form_submit.
Change:
'onclick="return con("Are you sure you don\'t have a configuration to submit? Please check the checkbox if you do.")'
To:
'onclick="return con(\'Are you sure you don\\\'t have a configuration to submit? Please check the checkbox if you do.\')"'
Isn't your code getting into endless loop?
You've redefined confirm()
JS function and you're executing you recurrently.
confirm is a JavaScript function, rename your function
精彩评论