douhu5837 2011-10-08 06:37
浏览 133
已采纳

带有重定向的JavaScript确认框

I need to ask a user to confirm the delete of his post. I managed it using the confirm function from jQuery but I wanted to create an overlay box with more customization options and after browsing the web I came up with this:

Info: CMS in question is Wordpress

So first the theme: index.php:

<li class="EditDel">
    <?php u_delete_post_link('Delete', '', ''); ?>
</li>

PHP function:

function u_delete_post_link($link = 'Delete This', $before = '', $after = '') {
    global $post;

    $message = "Are you sure you want to delete ".get_the_title($post->ID)." ?";
    $delLink = wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&amp;post=" . $post->ID, 'delete-post_' . $post->ID);
    $htmllink = "<a href='' onclick = \" u_ask_go('".$message."','".$delLink."' ) \"/>".$link."</a>";
    echo $before . $htmllink . $after;
}

Now for the actual problem the link above fires a js function with the message and the link:

function u_ask_go(msg,link)
{   
    u_confirm(msg,link, function () {
        window.location.href = arguments[0];
    });
}

and the u_confirm function:

function u_confirm(message, link, callback) {
    jQuery('#confirm').modal({
        closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
        position: ["20%",],
        overlayId: 'confirm-overlay',
        containerId: 'confirm-container', 
        onShow: function (dialog) {
            var modal = this;

            modal.link = link;

            //console.log(modal.link);

            jQuery('.message', dialog.data[0]).append(message);


            // if the user clicks "yes"
            jQuery('.yes', dialog.data[0]).click(function () {
                // call the callback

                if (jQuery.isFunction(callback)) {
                    //console.log(modal.link);
                    callback.apply(modal.link);
                }
                // close the dialog
                modal.close(); // or $.modal.close();
            });
        }
    });
}

Problem1: The callback function does not fire

Problem2: The dialog collapses and the link executes without user pressing anything

Observation: Before the modal div is appended the link variable is visible but not in the function on the dialog yes button seems not visible

Hope someone can help

EDIT (HTML buildup):

    <!-- modal content -->
    <div id='confirm'>
        <div class='header'><span>Confirm</span></div>
        <div class='message'></div>
        <div class='buttons'>
            <div class='no simplemodal-close'>No</div><div class='yes'>Yes</div>
    </div>
    </div>
    <!-- preload images -->
    <div style='display:none'>
        <img src='img/confirm/header.gif' alt='' />
        <img src='img/confirm/button.gif' alt='' />
    </div>

EDIT2:

echo wp_enqueue_script('simplemodal', $blogroot.'/js/jquery.js');
echo wp_enqueue_script('simplemodal', $blogroot.'/js/jquery.simplemodal.js', array("jquery"));
echo wp_enqueue_script('confirmtest', $blogroot.'/js/confirmtest.js', array("jquery"));
  • 写回答

2条回答 默认 最新

  • douqiao7958 2011-10-08 16:12
    关注

    The best thing to do is not to put your function in an onClick="" attribute, you should use jQuery to call the function like so, and store your variables in custom data attributes (info here)

    jQuery:

    $('a#confirmDelete').live('click', function(e) {
    
        // Prevent default
        e.preventDefault();
    
        var msg = $(this).attr('data-message');
        var link = $(this).attr('data-del-link');
    
        u_confirm(msg,link, function () {
            window.location.href = arguments[0];
        });
    });
    

    HTML:

    function u_delete_post_link($link = 'Delete This', $before = '', $after = '') {
        global $post;
    
        $message = "Are you sure you want to delete ".get_the_title($post->ID)." ?";
        $delLink = wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&amp;post=" . $post->ID, 'delete-post_' . $post->ID);
        $htmllink = "<a href="#" id="confirmDelete" data-message="' . $message . '" data-del-link="' . $delLink . '" />".$link."</a>";
        echo $before . $htmllink . $after;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应