0

我的 HTML 代码是,

 <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;
         }
      }
 });

显示我的确认消息,但如果用户确认,则它没有进入删除页面。谁能帮帮我。

4

2 回答 2

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>
于 2013-09-26T07:06:57.070 回答
0

您需要在ajax调用之后添加return false并在ajax调用中处理不同的结果因为ajax调用是异步的,因此它不会等待它返回true/false

编辑这可能是您正在寻找的

$('.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;
 });
于 2013-09-26T08:30:01.770 回答