0
我在这里遇到问题。我刚开始学习Yii2
,我需要大写first letters
我的参赛表格(它的名字和姓氏)。我用JS做这件事,但不知何故,我的代码不起作用,并没有打印任何结果。有人能帮我解决这个问题,并告诉我我做错了什么吗?在JavaScript中首字母大写
这里是我的代码:
function entryForm(name, surname) {
this.name = $(name);
this.surname = $(surname);
var self = this;
/*
* Capitalizes first letter of users entered name and surname
*
* @param string input
*/
this.capitalizeName = function(input) {
var name = input.val();
name = name.toLowerCase().replace(/(^|\s)[a-z]/g, function(letter) {
return letter.toUpperCase();
})
input.val(name);
}
var entryForm = new entryForm('#employee-name', '#employee-surname');
$(document).ready(function() {
/*
* Executes named function
*/
$('#employee-name, #employee-surname').change(function() {
entryForm.capitalizeName($(this));
})
})
}
您可以使用解决方案从http://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript – Bizley
待办事项你需要显示'大写'的文本,或者它不是必需的(这足以将它存储在数据库中)? – gmc
@gmc我需要以大写字母自动显示enterted名称。是的,我可以通过添加规则来做到这一点,但不是自动替换它,而是在点击“创建”按钮后替换字母 – user7435747