1

Javascript では、フィールドに入力された氏名 (First、Middle、および Last) を取得しようとしています。送信ボタンをクリックすると、文字の長さ、ミドルネーム、および 3 つのイニシャルの 3 つの個別のフィールドに出力されます。これまで文字フィールドにたどり着きましたが、2 つのスペースの間にある情報を取得する方法に困惑しています。

// 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;
4

1 に答える 1

1

これは宿題のように聞こえるので、学習体験を損なうことなくいくつかのヒントを紹介します。

文字列で使用split(' ')して、その文字列内の (スペースで区切られた) 単語の配列を取得できます。

文字列で使用charAt(0)して、文字列の最初の文字を取得できます。

于 2012-10-23T13:58:14.143 に答える