在Javascript中,我想要輸入一個字段的全名(First,Middle和Last),並且一旦點擊提交按鈕以輸出到三個單獨的字段,字符長度,中間名字,和3個首字母縮寫。到目前爲止,我已經到了角色領域,但我很難理解如何獲取兩個空間之間的信息。中間名第一個javascript
// 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;
推薦閱讀:[Falsehoods程序員相信姓名](http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/) - 你會很驚訝你的錯誤*「全名=第一,中間和最後」* - 假設是。 (隱式推薦:不要) – Tomalak
除了Tomalak [完全正確]的觀察外,側邊欄還有一個相關問題:http://stackoverflow.com/questions/4276905/javascript-regular-expression-to-attempt-拆分名稱到名稱的名字 –