如果选中绑定事件,则添加插件选项

时间:2013-02-27 23:21:57

标签: jquery

我正在尝试向this stringToSlug plugin添加一个额外的选项,因此如果选中该选项以打开/关闭插件,则仅绑定事件。

E.g我有表格字段:

  1. 标题
  2. 网址
  3. 自动网址(复选框)?
  4. 当我添加if($(checkbox).is(":checked"))时,这仅适用于首次加载,而不是当您取消选中/欺骗复选框时,stringToSlug插件无法再次开始工作。

    插件

    /*
     * jQuery stringToSlug plug-in 1.2.1
     *
     * Plugin HomePage http://leocaseiro.com.br/jquery-plugin-string-to-slug/
     *
     * Copyright (c) 2009 Leo Caseiro
     * 
     * Based on Edson Hilios (http://www.edsonhilios.com.br/ Algoritm
     * 
     *
     * Dual licensed under the MIT and GPL licenses:
     *   http://www.opensource.org/licenses/mit-license.php
     *   http://www.gnu.org/licenses/gpl.html
     */
    
    jQuery.fn.stringToSlug = function(options) {
        var defaults = {
            setEvents: 'keyup keydown blur', //set Events that your script will work
            getPut: '#permalink', //set output field
            space: '-', //Sets the space character. If the hyphen,
            prefix: '',
            suffix: '',
            replace: '' //Sample: /\s?\([^\)]*\)/gi
        };
    
        var opts = jQuery.extend(defaults, options);
    
        jQuery(this).bind(defaults.setEvents, function () {
            var text = jQuery(this).val();
            text = defaults.prefix + text + defaults.suffix; //Concatenate with prefix and suffix       
            text = text.replace(defaults.replace, ""); //replace
            text = jQuery.trim(text.toString()); //Remove side spaces and convert to String Object
    
            var chars = []; //Cria vetor de caracteres
            for (var i = 0; i < 32; i++) {
                chars.push ('');
            }   
    
            /*** Abaixo a lista de caracteres ***/
            chars.push (defaults.space); // Unicode 32
            chars.push ('');   // !
            chars.push ('');   // "
            chars.push ('');   // #
            chars.push ('');   // $
            chars.push ('');   // %
            chars.push ('');   // &
            chars.push ("");   // '
            chars.push (defaults.space);  // (
            chars.push (defaults.space);  // ); 
            chars.push ('');   // *
            chars.push ('');   // +
            chars.push (defaults.space);  // ); 
            chars.push (defaults.space);  // -
            chars.push (defaults.space);  // .
            chars.push (defaults.space);  // /
            chars.push ('0');  // 0
            chars.push ('1');  // 1
            chars.push ('2');  // 2
            chars.push ('3');  // 3
            chars.push ('4');  // 4
            chars.push ('5');  // 5
            chars.push ('6');  // 6
            chars.push ('7');  // 7
            chars.push ('8');  // 8
            chars.push ('9');  // 9
            chars.push ('');   // :
            chars.push ('');   // ;
            chars.push ('');   // <
            chars.push ('');   // =
            chars.push ('');   // >
            chars.push ('');   // ?
            chars.push ('');   // @
            chars.push ('A');  // A
            chars.push ('B');  // B
            chars.push ('C');  // C
            chars.push ('D');  // D
            chars.push ('E');  // E
            chars.push ('F');  // F
            chars.push ('G');  // G
            chars.push ('H');  // H
            chars.push ('I');  // I
            chars.push ('J');  // J
            chars.push ('K');  // K
            chars.push ('L');  // L
            chars.push ('M');  // M
            chars.push ('N');  // N
            chars.push ('O');  // O
            chars.push ('P');  // P
            chars.push ('Q');  // Q
            chars.push ('R');  // R
            chars.push ('S');  // S
            chars.push ('T');  // T
            chars.push ('U');  // U
            chars.push ('V');  // V
            chars.push ('W');  // W
            chars.push ('X');  // X
            chars.push ('Y');  // Y
            chars.push ('Z');  // Z
            chars.push (defaults.space);  // [
            chars.push (defaults.space);  // /
            chars.push (defaults.space);  // ]
            chars.push ('');   // ^
            chars.push (defaults.space);  // _
            chars.push ('');   // `
            chars.push ('a');  // a
            chars.push ('b');  // b
            chars.push ('c');  // c
            chars.push ('d');  // d
            chars.push ('e');  // e
            chars.push ('f');  // f
            chars.push ('g');  // g
            chars.push ('h');  // h
            chars.push ('i');  // i
            chars.push ('j');  // j
            chars.push ('k');  // k
            chars.push ('l');  // l
            chars.push ('m');  // m
            chars.push ('n');  // n
            chars.push ('o');  // o
            chars.push ('p');  // p
            chars.push ('q');  // q
            chars.push ('r');  // r
            chars.push ('s');  // s
            chars.push ('t');  // t
            chars.push ('u');  // u
            chars.push ('v');  // v
            chars.push ('w');  // w
            chars.push ('x');  // x
            chars.push ('y');  // y
            chars.push ('z');  // z
            chars.push ('Z'); 
            chars.push ('z'); 
    
            for (var i = 256; i < 100; i++) {
                chars.push ('');
            }
    
            var stringToSlug = new String (); //Create a stringToSlug String Object
            for (var i = 0; i < text.length; i ++) {
                stringToSlug += chars[text.charCodeAt (i)]; //Insert values converts at slugs
            }
    
            stringToSlug = stringToSlug.replace (new RegExp ('\\'+defaults.space+'{2,}', 'gmi'), defaults.space); // Remove any space character followed by Breakfast
            stringToSlug = stringToSlug.replace (new RegExp ('(^'+defaults.space+')|('+defaults.space+'$)', 'gmi'), ''); // Remove the space at the beginning or end of string
    
            stringToSlug = stringToSlug.toLowerCase(); //Convert your slug in lowercase     
    
    
            jQuery(defaults.getPut).val(stringToSlug); //Write in value to input fields (input text, textarea, input hidden, ...)
            jQuery(defaults.getPut).html(stringToSlug); //Write in HTML tags (span, p, strong, h1, ...)
    
    
            return this;
        });
    
      return this;
    }
    

1 个答案:

答案 0 :(得分:1)

可能有几种方法可以执行此操作,但您基本上需要添加一个处理程序,用于单击激活stringToSlug处理程序的“自动URL”复选框,并在执行转换之前选中复选框。

我是通过向插件添加选项'check'来实现此目的的,该插件是“自动URL”复选框的选择器。然后我使用它来附加一个处理程序,它触发绑定到setEvents的处理程序。我刚刚添加了一个名为stringtoslug的自定义事件来完成此任务。在处理程序内部,如果未选中复选框,则只返回,不执行任何操作。

jQuery.fn.stringToSlug = function(options) {

    var defaults = {
        setEvents: 'keyup keydown blur', //set Events that your script will work
        getPut: '#permalink', //set output field
        check: undefined,  // Checkbox
        space: '-', //Sets the space character. If the hyphen,
        prefix: '',
        suffix: '',
        replace: '' //Sample: /\s?\([^\)]*\)/gi
    };

    var opts = jQuery.extend(defaults, options);

    var source = this; // save "this" for triggering

    // If a checkbox was defined, add a handler to it
    if(defaults.check !== undefined) {

        // When the checkbox changes, custom fire a "stringtoslug" event
        jQuery(defaults.check).on('change', function() {
            jQuery(source).trigger('stringtoslug');
        });
    }

    // Add a "stringtoslug" event listener to the others
    jQuery(this).bind(defaults.setEvents + ' stringtoslug', function () {

        // If the checkbox is defined and it is not checked, don't do anything
        if(defaults.check !== undefined && !jQuery(defaults.check).prop('checked')) {
            return;    
        }

        // ...EVERYTHING ELSE IS THE SAME...
    }
}

并称之为:

$('#title').stringToSlug({
    getPut: '#url',
    check: '#autourl'
});

演示:http://jsfiddle.net/jtbowden/KJzvZ/