简体   繁体   English

JavaScript表单值-用变量替换值并计算变量总和

[英]JavaScript Form Values - Replace Value with Variable & Calculate Sum of Variables

I've been searching for a solution on this but haven't been able to find anything I can understand. 我一直在寻找解决方案,但找不到任何我能理解的东西。

I'm working on the piece of code below - I have simplified the output for ease of reading. 我正在下面的代码上-为了简化阅读,我简化了输出。

function CreateMsg() { 
var MsgDOM = document.getElementById("MSG"); 

MsgDOM.innerHTML = '<p>calculation.value</p><li>How you view God or a Higher Power: ' + document.forms[0].godview.value + '<\/li><li>How you view your life in general: ' + document.forms[0].lifeview.value + '<\/li><li>How you approach life: ' + document.forms[0].level.value + '<\/li><li>What your inner voice and internal thoughts are busy with: ' + document.forms[0].emotion.value + '<\/li><li>How you are most likely to tackle challenges: ' + document.forms[0].process.value + '<\/li><li>The lessons (mirrors) you are currently working with: ' + document.forms[0].lessons.value + '<\/li>'; 
}

I have everything working fine so far, but I need to do a calculation that reads the 6 values that the output display is picking up. 到目前为止,我一切正常,但是我需要进行一次计算,以读取输出显示正在拾取的6个值。

The value has to be read, and then replaced from an array - the same array applies to all six variables. 必须先读取该值,然后从数组中替换该值-同一数组适用于所有六个变量。

So the output value for each of the elements in the output is one of a number of fixed text values, usually a number. 因此,输出中每个元素的输出值是许多固定文本值(通常是数字)之一。 So if the value is 1, for example, I want it to replace that value with a 2. If it's 2, then replace it with 4, etc. There's a string of about 20 values, but they are common to all six numbers I need to calculate. 因此,例如,如果值是1,我希望它用2替换该值。如果是2,然后将其替换为4,以此类推。有大约20个值的字符串,但是它们对于所有六个数字都是通用的需要计算。

Then I want it to take those six values and add them together, and print them to the output display where it says calculation.value. 然后,我希望它采用这六个值并将它们加在一起,然后将它们打印到输出显示中,上面显示“ calculation.value”。

That should be reasonably easy to do, something like this should be what you're looking for! 那应该很容易做到,像这样的东西应该是您想要的!

 function CreateMsg() { var MsgDOM = document.getElementById("MSG"); let fieldsToRead = ["godview", "lifeview", "level", "emotion", "process", "lessons"]; let inputArray = fieldsToRead.map((fieldName) => { return Number(document.forms[0][fieldName].value); }); console.log(inputArray); // This determines how we map input fields to output fields. // We can add more elements as needed. let inputOutputMap = { 0:0,1:2,2:4,3:6,4:8,5:10,6:12 }; // You can perform your calculations here!! let result = inputArray.reduce((total, input) => { total += (inputOutputMap[input] || 0); return total; }, 0); MsgDOM.innerHTML = '<p>Calculation.value</p><li>How you view God or a Higher Power: ' + document.forms[0].godview.value + '<\\/li><li>How you view your life in general: ' + document.forms[0].lifeview.value + '<\\/li><li>How you approach life: ' + document.forms[0].level.value + '<\\/li><li>What your inner voice and internal thoughts are busy with: ' + document.forms[0].emotion.value + '<\\/li><li>How you are most likely to tackle challenges: ' + document.forms[0].process.value + '<\\/li><li>The lessons (mirrors) you are currently working with: ' + document.forms[0].lessons.value + '<\\/li><br><h3>Calculation total: ' + result + '</h3>'; } CreateMsg(); 
 <html> <body> <form> Godview:<br> <input type="text" name="godview" value="1"><br> Lifeview:<br> <input type="text" name="lifeview" value="2"><br> Level:<br> <input type="text" name="level" value="3"><br> Emotion:<br> <input type="text" name="emotion" value="4"><br> Process:<br> <input type="text" name="process" value="5"><br> Lessons:<br> <input type="text" name="lessons" value="6"><br> </form> <div> <br> <br> <div id="MSG"> </div> </body> </html> 

 hi I go through code acording the requirement bellow is the solved code can sum the values from the array <!DOCTYPE HTML> <html> <head> </head> <body onload=""> <form> <input type="text" id="godview" value="" /> <input type="text" id="lifeview" value="" /> <input type="text" id="level" value="" /> <input type="text" id="emotion" value="" /> <input type="text" id="process" value="" /> <input type="text" id="lessons" value="" /> </form> <div> <input type="button" id="btnsend" value="save" onclick="return CreateMsg();" /> </div> <div id="MSG"> </div> <script> function CreateMsg() { var arr = []; var godv = document.forms[0].godview.value; var lifv = document.forms[0].lifeview.value; var levelv = document.forms[0].level.value; var emotionv = document.forms[0].emotion.value; var processv = document.forms[0].process.value; var lessonsv = document.forms[0].lessons.value; arr.push(godv); arr.push(lifv); arr.push(levelv); arr.push(emotionv); arr.push(processv); arr.push(lessonsv); alert(arr); var MsgDOM = document.getElementById("MSG"); MsgDOM.innerHTML = '<p>calculation.value</p><li>How you view God or a Higher Power: ' + document.forms[0].godview.value + '<\\/li><li>How you view your life in general: ' + document.forms[0].lifeview.value + '<\\/li><li>How you approach life: ' + document.forms[0].level.value + '<\\/li><li>What your inner voice and internal thoughts are busy with: ' + document.forms[0].emotion.value + '<\\/li><li>How you are most likely to tackle challenges: ' + document.forms[0].process.value + '<\\/li><li>The lessons (mirrors) you are currently working with: ' + document.forms[0].lessons.value + '<\\/li>'; } </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM