javascript - 如何在javascript中组合两个id

标签 javascript jquery forms slug

目前我正在开发一个应用程序供我妻子使用,由于她不懂计算机,我试图让她更容易解决问题,也让我更容易在将来发现任何错误。

所以我发现一个slug generator基于jquery,可以自动将带有id标签的输入框传递到slug框。我相信这对我来说最有用,因为我的妻子可以跳过 slug 上的选项,而不是输入不相关的 slug。

这是 JavaScript:

(function ($) {
    // DONT FORGET TO NAME YOUR PLUGIN
    jQuery.fn.makeSlug = function (options, i) {
        if (this.length > 1) {
            var a = new Array();
            this.each(
                function (i) {
                    a.push($(this).makeSlug(options, i));
                });
            return a;
        }
        var opts = $.extend({}, $().makeSlug.defaults, options);

        /* PUBLIC FUNCTIONS */

        this.destroy = function (reInit) {
            var container = this;
            var reInit = (reInit != undefined) ? reInit : false;
            $(container).removeData('makeSlug'); // this removes the flag so we can re-initialize
        };

        this.update = function (options) {
            opts = null;
            opts = $.extend({}, $().makeSlug.defaults, options);
            this.destroy(true);
            return this.init();
        };

        this.init = function (iteration) {
            if ($(container).data('makeSlug') == true)
                return this; // this stops double initialization

            // call a function before you do anything
            if (opts.beforeCreateFunction != null && $.isFunction(opts.beforeCreateFunction))
                opts.beforeCreateFunction(targetSection, opts);

            var container = this; // reference to the object you're manipulating. To jquery it, use $(container). 

            $(container).keyup(function(){
                if(opts.slug !== null) opts.slug.val(makeSlug($(this).val()));
            });

            $(container).data('makeSlug', true);

            // call a function after you've initialized your plugin
            if (opts.afterCreateFunction != null && $.isFunction(opts.afterCreateFunction))
                opts.afterCreateFunction(targetSection, opts);
            return this;
        };

        /* PRIVATE FUNCTIONS */

        function makeSlug(str) { 
            str = str.replace(/^\s+|\s+$/g, ''); // trim
            str = str.toLowerCase();

            // remove accents, swap ñ for n, etc
            var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
            var to   = "aaaaeeeeiiiioooouuuunc------";
            for (var i=0, l=from.length ; i<l ; i++) {
                str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
            }

            str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
            .replace(/\s+/g, '-') // collapse whitespace and replace by -
            .replace(/-+/g, '-'); // collapse dashes

            return str;
        };

        // Finally
        return this.init(i);
    };

    // DONT FORGET TO NAME YOUR DEFAULTS WITH THE SAME NAME
    jQuery.fn.makeSlug.defaults = {
        slug: null,
        beforeCreateFunction: null, // Remember: these are function _references_ not function calls
        afterCreateFunction: null
    };
})(jQuery);

这是调用脚本代码:

<script type="text/javascript">
<!--
    $(document).ready(function(){
    $('#fname').makeSlug({
        slug: $('#slug')
    });
});
-->
</script>

这是我的表格:

   <span class="label">First Name:</span> <input type="text" name="fname" maxlength="20" id="fname" placeholder="First Name" />
    <br /><br />
    <span class="label">Last Name :</span> <input type="text" name="lname" maxlength="20" id="lname" placeholder="Last Name" />
    <br /><br />
    <span class="label">Slug :</span> <input type="text" name="slug" maxlength="20" id="slug" placeholder="Slug" />   
    <br /><br />

我想要做的是,名字和姓氏输入组合并在 slug 输入框中生成。

例如:

名字:杰夫

姓:杰米·海登

鼻涕虫:杰夫-杰米-海登

但是当前的脚本仅适用于我的名字标签,我也想添加姓氏,请告诉我在哪里以及如何实现它。

非常感谢所有花时间阅读和理解我的问题并帮助我的 SO 大师。

最佳答案

低落又肮脏?这取代了 slug 插件。

$('#fname,#lname').keyup(function() { // *FANCIER* using this would trigger slug creation on the fly as you type either first OR last name
$('#lname').blur(function() { // *SIMPLER* this triggers the slug to create when the last name has been typed
    var f = $('#fname').val(); // get the first name
    f = f.toLowerCase(); // convert it to lower case
    f = f.replace(/[^a-z0-9 -]/g, ''); // remove invalid characters
    f = f.replace(/\s+/g, '-'); // replace whitespace with dash
    f = f.replace(/-+/g, '-'); // replace space with dash
    var l = $('#lname').val();
    l = l.toLowerCase();
    l = l.replace(/[^a-z0-9 -]/g, '');
    l = l.replace(/\s+/g, '-');
    l = l.replace(/-+/g, '-');
    var s = f + '-' + l; // concatenate resulting first and last to slug
    $('#slug').val(s).attr('readonly','readonly');
});

JSFiddle

enter image description here

关于javascript - 如何在javascript中组合两个id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708423/

相关文章:

javascript - 有人可以告诉我如何在 Unix 上运行 Node.js 吗?

javascript - onclick ="submit();"在不同浏览器中的不同行为

php - Codeigniter 3.1.0 Active Record 更新查询既不出错也不更新数据库

javascript - Highcharts:动态数据的符号标记?

javascript - 为什么新的 ES6 find 方法在 JSBin 中无法识别?

javascript - 金属史密斯 : How to build to same directory as build script?

jquery - 使用 JQUERY 关闭一个 DIV 标签并打开一个新的 DIV 标签

javascript - 确定提交表单时的事件输入

validation - Jquery 验证插件和 Colorbox

javascript - 滚动两个具有停靠效果的 div