目前我正在开发一个供我妻子使用的应用程序,因为她不是一个精通计算机的人,我试图让她更容易锻炼,以便我以后更容易发现任何错误。< / p>
所以我发现基于jquery的slug generator,它可以自动传输带有标记为slug框的id的输入框。我相信这对我来说最有用,因为我的妻子可以跳过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 />
我打算做的是,名字&amp;姓氏输入在slug输入框中组合并生成。
例如:
名字:杰夫
姓氏:Jamie Hayden
Slug:jeff-jamie-hayden
但是当前的脚本只适用于我的名字标签,我想在姓氏中加入,请告诉我在哪里以及如何实现它。
非常感谢所有那些花时间阅读和理解我的问题并帮助我的SO大师。
答案 0 :(得分:1)
沮丧和肮脏?这取代了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');
});