2013-09-26 5 views
0

내 HTML 코드는 삭제 페이지로 이동, 대신에이 경고의 jquery에서 다시 기본 설정을 사용하지 않도록 설정하는 방법은 무엇입니까?

<a title="Delete" onclick="if (confirm('Delete selected item?')){ return true; }else{ event.stopPropagation(); event.preventDefault();};" class="delete" href="link to delete page"> 
</a> 

, 나는 특정 조건에 해당 사용자에 본사를 둔 내 경고 기능을 제공 원하는 옵션을 확인한다. 내 jQuery 코드는

,

$('.delete').removeAttr('onclick').click(function(e) { 
    e.preventDefault(); 
    var url = $(this).attr("href"); 
    var a = url.split('id_product='); 
    var next = a[1]; 
    var id=next.split('&');  
    var base_url = $("#ajaxbase").val(); 
    $.ajax({ 
     type : "POST",  
     url : base_url+"modules/sample/ajax/sample.php", 
     data: {id:id[0]}  
    }).done(function(response) { 
     var res=$.trim(response); 
     if (res == '1') { 
     if (confirm('This Product is configured in chain.Deleting this product will affect your chain.Are you sure to do this?')) { 
     return true;    
     } else { 
     return false; 
     } 
     } else if (res == '2') { 
     if (confirm('Are you sure to delete this product?')) { 
     return true; 
     } else { 
     return false; 
     } 
     } 
}); 

내 확인 메시지가 표시되지만 사용자가이를 확인하면, 그것은 삭제 페이지로 이동하지 않았다. 누구든지 나를 도울 수 있습니까?

+0

무엇을 찾고있을 수 있습니다, 나는 버튼의 동작을 모방하는 태그를 사용, 당신은 완벽하게 제어 할 것 ! – MackieeE

답변

0

앵커 요소가 아닌 <a>을 더 잘 제어하면이 효과를 얻을 수 있습니다. 일반적으로 주어진 요소의 동작을 완벽하게 제어하려는 경우 좋으며, 단지 클리너 일뿐입니다.

원래 들여 쓰기 된 동작을 계속하려면 끝에 window.location.href을 적어 두는 것이 중요합니다.

<span class="delete" data-linkto="somepage.php">Delete</span> 

<script> 
    $('.delete').on('click', function() { 

    var 
     url = $(this).data("linkto"), 
     a = url.split('id_product='), 
     next = a[1], 
     id = next.split('&'), 
     base_url = $("#ajaxbase").val(), 
     verifyIfOne = confirm('This Product is configured in chain. '+ 
           'Deleting this product will affect your chain. '+ 
           'Are you sure to do this?'), 
     verifyIfTwo = confirm('Are you sure to delete this product?'), 
     Accepted = false; 

     $.ajax({ 

      //Headers 
      type : "POST",  
      url : base_url + "modules/sample/ajax/sample.php", 
      data: { id:id[0] }  

     }).done(function(response) { 

      var res = $.trim(response); 

      if (res == '1') { 
      if (verifyIfOne) { 
       Accepted = true;   
      } 
      } 

      if (res == '2') { 
      if (verifyIfTwo) { 
       Accepted = true; 
      } 
      } 
    /** 
     * User has agreed to go through with 
     * the deletion. 
    **/ 
    if (Accepted) 
     window.location.href = url; 

    }); 
</script> 
0

당신은/거짓 아약스 호출 후 false를 반환 추가하고 다른 AJAX 호출의 결과를 처리하는 AJAX 호출이 asyn이기 때문에 그것은 true를 반환하는 것이 기다리지 않습니다 그 때문에 필요

이 이벤트 버블 링의의 편집 이것은 당신이 일반적으로 이와 같은 버튼

$('.delete').removeAttr('onclick').click(function(e) { 
    e.preventDefault(); 
    var url = $(this).attr("href"); 
    var a = url.split('id_product='); 
    var next = a[1]; 
    var id=next.split('&');  
    var base_url = $("#ajaxbase").val(); 
    $.ajax({ 
     type : "POST",  
     url : base_url+"modules/sample/ajax/sample.php", 
     data: {id:id[0]}  
    }).done(function(response) { 
     var res=$.trim(response); 
     if (res == '1') { 
     if (confirm('This Product is configured in chain.Deleting this product will affect your chain.Are you sure to do this?')) { 
      window.location.href = url ; 
     } 
     } else if (res == '2') { 
     if (confirm('Are you sure to delete this product?')) { 
      window.location.href = url ; 
     } 
     } 
    return false; 
}); 
관련 문제