复制(模仿)OpenCart确认删除

时间:2014-07-20 05:23:40

标签: javascript jquery button opencart

我想复制OpenCart的jQuery以确认操作。这是javascript:

<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 个答案:

答案 0 :(得分: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

内的字符串