1

我正在注入一些 jQuery 来制作 WordPress 上传厚框所需的 Alt Text 字段。

它拦截“插入到帖子”按钮单击并检查该字段是否已填充。

它适用于图库媒体库选项卡,但从计算机选项卡需要一个“侦听器”,当上传完成以更改插入到帖子按钮的行为时。

在此处输入图像描述



我正在尝试setInterval,但不知道如何杀死重新创建它,但也许有人知道是否存在侦听器,或者甚至如何使这段代码工作,因为我怀疑我这里的逻辑是模糊的......

这是代码,已评论。

add_action('admin_head-media-upload-popup','so_11149675_required_alt_text');

function so_11149675_required_alt_text()
{
    // Detect current tab ("From Computer" == "type")
    $tab = isset($_GET['tab']) ? $_GET['tab'] : "type";

    // ( 'From Computer' or ( 'Gallery' and 'Library' ) )
    $jquery = ('type' == $tab) ? 'var refreshUpload = setInterval(function(){$(".savesend input").each(checkAltTextPermanent);},500);' : '$(".savesend input").each(checkAltTextOnce);';

    echo <<<HTML
    <script language="javascript" type="text/javascript">
        // var refreshUpload; /* testing */

        // Function called by From Computer tab
        // should run only once -> when the upload table and fields are created
        function checkAltTextPermanent() {
            // Create the required asterisk symbol
            // setInterval creates a loop here
            jQuery('.image_alt th label').each(function(i,e) {
                jQuery('<span class="alignright"><abbr title="required" class="required">*</abbr></span>').prependTo(this);
            }); 

            // Alter button behavior
            // Another loop in the alert box
            jQuery(this).click(function(e) {
                // clearInterval(refreshUpload);
                var value = jQuery(this).parent().parent().parent().find('.image_alt input').val();

                if('' != value) 
                    return true;

                alert ('Please fill the Alt text');
                return false;
            });
        }

        // Function called by Gallery and Library tabs
        function checkAltTextOnce() {
            jQuery(this).click(function(e) {
                var value = jQuery(this).parent().parent().parent().find('.image_alt input').val();

                if('' != value) 
                    return true;

                alert ('Please fill the Alt text');
                return false;
            });
        }

        jQuery(document).ready(function($) {
            // Defined in PHP, calls checkAltTextOnce or checkAltTextPermanent
            {$jquery}

            // Used in Gallery and Libray tabs
            $('.image_alt th label').each(function(i,e) {
                $('<span class="alignright"><abbr title="required" class="required">*</abbr></span>').prependTo(this);
            });
        });
    </script>
HTML;
}
4

1 回答 1

1

试试这个:

jQuery(".savesend input").live("click", validateAltText);

function validateAltText() {
    var value = $(".image_alt input").val();

    if (value) 
        return true;

    alert('Please fill the Alt text');
    return false;
}
于 2012-06-22T03:21:47.697 回答