0

我正在尝试为此 stringToSlug 插件添加一个额外的选项,因此只有在选中该选项以打开/关闭插件时才绑定事件。

例如,我有表单字段:

  1. 标题
  2. 网址
  3. 自动 URL(复选框)?

当我添加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;
}
4

1 回答 1

1

可能有几种方法可以做到这一点,但您基本上需要添加一个处理程序来单击触发 stringToSlug 处理程序的“自动 URL”复选框,并在进行转换之前选中该复选框。

我通过在插件中添加一个选项“检查”来做到这一点,该插件是“自动 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/

于 2013-02-28T00:40:17.137 回答