简体   繁体   English

在OnChange event Javascript中需要帮助

[英]Need help in OnChange event Javascript

I have five text fields which are mark1, mark2, mark3, mark4 and total. 我有五个文本字段,分别是mark1,mark2,mark3,mark4和total。 I have a problem to make the total automatically display in the text field. 我有一个问题是使总数自动显示在文本字段中。 I have no problem with the calculation part. 我对计算部分没有任何问题。 But I can't make the total to be display automatically in the text field. 但我无法在文本字段中自动显示总数。 How can I make the total to be automatically display in the textfield once the user have finish entered all the four marks? 一旦用户完成所有四个标记后,如何使总数自动显示在文本字段中? I know it have to do with the OnChange event using Javascript. 我知道它与使用Javascript的OnChange事件有关。 But I did not get the right codes. 但我没有得到正确的代码。 Does anyone can help me with this? 有没有人可以帮我这个? Really need your advise. 真的需要你的建议。

Thank you. 谢谢。

Daisy, something like this should work. 黛西,这样的事情应该有效。 You can view a live demo of this code on JSBin. 您可以在JSBin上查看此代码的实时演示

<input type="text" name="mark1" id="mark1" value="" />
<input type="text" name="mark2" id="mark2" value="" />
<input type="text" name="mark3" id="mark3" value="" />
<input type="text" name="mark4" id="mark4" value="" />
<input type="text" name="total" id="total" value="" />

<!-- Script block must come AFTER the input elements -->

<script type="text/javascript">
  var m1    = document.getElementById('mark1'),
      m2    = document.getElementById('mark2'),
      m3    = document.getElementById('mark3'),
      m4    = document.getElementById('mark4'),
      total = document.getElementById('total');

  function calculate_total(){
    // Use your real calculation here
    total.value = Number(m1.value) + Number(m2.value) + Number(m3.value) + Number(m4.value);
  }

  if( window.addEventListener ) {
    m1.addEventListener('change', calculate_total, false);
    m2.addEventListener('change', calculate_total, false);
    m3.addEventListener('change', calculate_total, false);
    m4.addEventListener('change', calculate_total, false);
  } else if(window.attachEvent){
    m1.attachEvent('onchange', calculate_total);
    m2.attachEvent('onchange', calculate_total);
    m3.attachEvent('onchange', calculate_total);
    m4.attachEvent('onchange', calculate_total);
  }
</script>

UPDATE: Since you want a letter grade entered (A, B, C, F, etc) I would recommend a select control instead of an input[type='text'] . 更新:由于您想要输入字母等级(A,B,C,F等),我建议使用select控件而不是input[type='text'] One of the grade fields would look like this: 其中一个等级字段如下所示:

<select name="mark1" id="mark1">
    <option value="">Select Grade</option>
    <option value="100">A</option>
    <option value="90">B</option>
    <option value="80">C</option>
    <option value="70">D</option>
    <option value="50">F</option>
</select>

You put the number value in the value= portion, but you can display the friendlier A, B, C, etc. 您将数字值放在value=部分,但您可以显示更友好的A,B,C等。

And anywhere there was .value replace with .selectedValue (Except total.value of course). 在任何地方都有.value替换为.selectedValue (当然除了total.value )。

Daisy, Doug provided a great answer , but after reading your comment I thought I would contribute a small change. 黛西, 道格提供了一个很好的答案 ,但在看完你的评论后,我想我会做出一点改变。 Note the grades array of values and the small change to Doug's function: 注意grades数组值和Doug函数的小变化:

// Add grades and corresponding value below
var grades = {A:100,B:70};

function calculate_total(){
  // Use your real calculation here
  total.value = grades[m1.value] + grades[m2.value] + grades[m3.value] + grades[m4.value];
}

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

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