onclick action on button works for only one condition
I am writing a php application with javascript and customized button. The problem is when the button is clicked, irrespective of clicking yes or cancel, it is doing a default function of clicking a 'yes'. Could you please help me in finding the error.
The php code is
<script type="text/javascript" language="javascript" src="javascript/access1.js"></script>
<form name="form_delete" id="form_delete" action="" method="POST" class="access">
|
|
|
<input type="hidden" name="deleteinstanceaction" value="1" />
</form>
<a id="_delete_btt" class="button" onClick="confirmDelete()">Delete</a>
<img class="ajaxload" style="display:none;" id="ajaxld" src="images/ajax-loader.gif"/>
Edit1 The javascript code is
function confirmDelete() {
var agree=confirm("Are you sure you wish to continue? This will remove the selected item permamently!");
if (agree)
return true ;
else
return false ;
}
Can the problem be related to AJAX? if I use normal button then this javascript works fine for e.g. in the below case this works fine on both 'yes' and 'no' click
<input type="submit" name="delete_state" value="Delete selected state" onClick="confirmDelete()">
Edit2 The code in the .js file is :
$(document).ready(function() {
Access.initEventHandle开发者_如何学运维rs(); });
var Access = {
initEventHandlers : function(){
if($('#_delete_btt')){
$("#_delete_btt").click(function(e){
Access.processSubmit();
});
}
processSubmit: function (event) {
$('#_delete_btt').hide();
$('#ajaxld').show();
setTimeout("Access.formsub()",500);
},
formsub: function () {
var url = 'php/corecontroller.php?ts='+new Date().getTime();
$.post(url, $('#form_delete').serialize(), Access.oncomplete,"json");
},
oncecomplete:function(data,textStatus){
if(textStatus == "success"){
if(data.result == "1"){
//sucessful
$('#ajaxld').hide();
htmlstr += "Deleted";
}
}
}
}
Could you please suggest how to proceed further?
As far as I know, returning false
in Javascript does not prevent a link to be followed if there is a href
attribute defined.
Since that's not your case I would define a href
attribute set to #
:
<a href="#" id="_delete_btt" class="button" onClick="confirmDelete()">Delete</a>
Do you have a click handler attached to the link by chance? Normally, returning false would indeed cancel the navigation.. but I don't think it cancels a click handler when attached like that.
So, if you did something like..
$("#_delete_btt").click(submitTheForm);
or
document.getElementById('_delete_btt').onclick(submitTheForm)
at a previous point, that will probably still submit the form as it isn't listening for a 'can I continue' from another bound callback. Assuming this is the case, bind a function to the 'a' tag that will confirm, and then process based on the user's response.
Edit:
Based on the new code you posted, we just need to change the program flow. Either put the confirmation in the click callback:
$("#_delete_btt").click(function(e){
if(confirmDelete()){
Access.processSubmit();
}
});
or put the code to process the submit in the confirmDelete:
function confirmDelete() {
var agree=confirm("Are you sure you wish to continue? This will remove the selected item permamently!");
if (agree){
Access.processSubmit();
return true ;
} else {
return false ;
}
}
Either way, what I had suspected was happening is in fact happening. The click event fires regardless of whether or not a callback returns false, causing the processSubmit to fire off as well.
精彩评论