2014-07-20 30 views
0

我想複製OpenCart的jQuery來確認操作。這是JavaScript:重複(Immitate)OpenCart確認刪除

<script type="text/javascript"> 
//----------------------------------------- 
// Confirm Actions (delete, uninstall) 
//----------------------------------------- 
$(document).ready(function(){ 
    // Confirm Delete 
    $('#form').submit(function(){ 
     if ($(this).attr('action').indexOf('delete',1) != -1) { 
      if (!confirm('<?php echo $text_confirm; ?>')) { 
       return false; 
      } 
     } 
    }); 
     }); 
    </script> 

的HTML是:

<a onclick="$('form').attr('action', '<?php echo $delete; ?>'); $('form').submit();" class="button"><?php echo $button_delete; ?></a> 

我的按鈕是:

<a onclick="$('form').attr('action', '<?php echo $process_carts_action; ?>'); $('form').submit();" class="button"><?php echo "Process Carts" ?></a> 

這是引用按鈕,怎麼行?

if ($(this).attr('action').indexOf('delete',1) != -1) 

回答

1

答案當然很簡單。

if ($(this).attr('action').indexOf('delete',1) != -1) 

上面的代碼轉換爲(請糾正我在哪裏,我錯了)

if // IF 
$(this) // this instance of the document object, form referenced by ID (#) 
.attr('action') // which has been assigned an attribute named 'action' 
.indexOf('delete',1) // we'll use the indexOf function to see if it contains string 'delete' 
!= -1 // and if it doesn't return "-1", which means 'false' 
if (!confirm('<?php echo $text_confirm; ?>')) //THEN 
// call the javascript 'confirm' function and fill it with text contained in php var $text_confirm 

因此,解決辦法是

$(document).ready(function(){ 
    // Confirm Process 
    $('#form').submit(function(){ 
     if ($(this).attr('action').indexOf('process_carts',1) != -1) { 
      if (!confirm("Are you sure you're ready to process all carts?")) { 
       return false; 
      } 
     } 
    }); 
}); 

哪裏process_carts是唯一一個字符串中#form是在變種$process_carts_action

+0

很好的答案。 +1 – shadyyx

+0

非常感謝您的幫助。 – MikeiLL