設置它與對象,所以你可以定義每個字母的值,如果需要的話。然後嘗試循環每個字母並獲取其數值乘以。這裏是一個DEMO
var o = { // Define Letter Values
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
g: 7,
h: 8,
i: 9,
j: 10,
k: 11,
l: 12,
m: 13,
n: 14,
o: 15,
p: 16,
q: 17,
r: 18,
s: 19,
t: 20,
u: 21,
v: 22,
w: 23,
x: 24,
y: 25,
z: 26
};
$('input').on('keyup', function(){ // Set whatever event you want to trigger the function
var str = $(this).val(); // Grab the string value
var arr = str.split(''); // Split the string on every letter into an array
var total = 0;
$.each(arr, function(i, v){ // Loop through the letters
var letterIndex = o[v]; // Get the numerical value of the current letter
if(i === 0){ // If this is the first letter set the starting letter value
total = letterIndex;
} else { // Else multiply the current total by the letter's value
total = total * letterIndex;
}
});
$('#yourOtherTextarea').val(total); // Do whatever you want with the results
});
希望這有助於!如果您有任何問題,請告訴我。
請詳細說明「a」,「b」和「c」的來源。他們是變數嗎?還是頁面元素? – Taplar
他們是變量 –