javascript - 名字 中间名 javascript

标签 javascript

在 Javascript 中,我尝试将全名(名字、中间名和姓氏)输入到字段中,单击提交按钮后将输出到三个单独的字段:字符长度、中间名和 3缩写。到目前为止,我已经到达字符字段,但我对如何获取两个空格之间的信息感到困惑。

// strings.js
// This script takes a name and spits out its character length with initials.

// Function called when the form is submitted.
// Function performs the calculation and returns false.
function formatNames() {
'use strict';

// For storing the Full Name, Middle name, initials, length:
var lengthName;
var middleName;
var yourInitials;


// Get a reference to the form values:
var fullName = document.getElementById('fullName').value;

// Perform the character spit out:
lengthName = fullName.length;

// Pull out the middle name:

// Display the volume:
document.getElementById('nameLength').value = lengthName;

// Return false to prevent submission:
return false;

} // End of formatNames() function.

// Function called when the window has been loaded.
// Function needs to add an event listener to the form.
function init() {
'use strict';
document.getElementById('theForm').onsubmit = formatNames;
} // End of init() function.
window.onload = init;

最佳答案

这听起来像是家庭作业,所以我会在不破坏学习体验的情况下给您一些提示。

您可以使用split(' ')在字符串上获取该字符串中的单词数组(用空格分隔)。

您可以使用charAt(0)在字符串上获取字符串的第一个字母。

关于javascript - 名字 中间名 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13031889/

相关文章:

javascript - 当 DOM 更改时,我是否需要清理我的事件处理程序?

javascript - React Hooks 表单 : undefined values on submit

javascript - getURLParameter 用于登陆页面

javascript - 如何在另一个元素的顶部显示导航

javascript - 在 vue.js 中更新数据属性/对象

javascript - 想要在单击元素时向该元素添加 CSS 类

javascript - 循环并组合每两个项目

Javascript比较算法

javascript - 我应该学习 ASP.NET AJAX、jQuery 还是两者都学?

javascript - Vee Validate 3.x,定义ValidationFlags 类型