我正在使用jquery mobile创建GPA计算器,但是在输入第二个模块的值后没有得到正确的结果,例如,当我将第一个模块的“学分”插入20且等级为“ A”时,然后按“计算”按钮I得到4.00,到目前为止还可以,但是当我再次向第二个模块插入20个学分,并且成绩为“ A”时,它没有给出正确的结果,表明结果为0.08,应该为4.00

这是我的脚本代码



$(document).ready(function(){
  $("#cal").click(function() {
    var a = document.getElementById('name1').value
    eval(a)
    var b = $('#grade1 option:selected').val()
    var c = document.getElementById('name2').value
    eval(c)
    var d = $('#grade2 option:selected').val()
    var e = document.getElementById('name3').value
    eval(e)
    var f = $('#grade3 option:selected').val()
    var g = document.getElementById('name4').value
    eval(g)
    var h = $('#grade4 option:selected').val()
    var i = document.getElementById('name5').value
    eval(i)
    var j = $('#grade5 option:selected').val()
    var m1 = a * b;
    var m2 = c * d;
    var m3 = e * f;
    var m4 = g * h;
    var m5 = i * j;
    var total1 = m1 + m2 + m3 + m4 + m5;
    var total2 = a + c + e + g + i;
    var total3 = total1/total2
    var total3 = total3.toFixed(2);

    $('#res').val(total3);
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div data-role="page">
  <div data-role="header" data-theme="f"> <a data-iconpos="notext" data-role="button" data-icon="home" title="Home">Home</a>
    <h1>BENG GPA Calculator</h1>
    <form id="form">
      <ul data-role="listview" data-inset="true">
        <li>Module 1</li>
        <li data-role="fieldcontain">
          <label for="name1">Credits:</label>
          <input type="text" name="name1" id="name1" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade1" class="select">Grade:</label>
          <select class="grade1" data-theme="f" id="grade1">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option
              >
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 2</li>
        <li data-role="fieldcontain">
          <label for="name2">Credits:</label>
          <input type="text" name="name2" id="name2" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade2" class="select">Grade:</label>
          <select class="grade2" data-theme="f" id="grade2">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 3</li>
        <li data-role="fieldcontain">
          <label for="name3">Credits:</label>
          <input type="text" name="name3" id="name3" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade3" class="select">Grade:</label>
          <select class="grade3" data-theme="f" id="grade3">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 4</li>
        <li data-role="fieldcontain">
          <label for="name4">Credits:</label>
          <input type="text" name="name4" id="name4" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade4" class="select">Grade:</label>
          <select class="grade4" data-theme="f" id="grade4">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 5</li>
        <li data-role="fieldcontain">
          <label for="name5">Credits:</label>
          <input type="text" name="name5" id="name5" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade5" class="select">Grade:</label>
          <select class="grade5" data-theme="f" id="grade5">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li data-role="fieldcontain">
          <label for="res">Result:</label>
          <input type="text" name="res" id="res" value="" data-clear-btn="true">
        </li>
        <li class="ui-body ui-body-b">
          <fieldset class="ui-grid-a">
            <div>
              <button type="button" id="cal" name="cal" data-theme="f">Calculate</button>
            </div>
          </fieldset>
        </li>
        <li class="ui-body ui-body-b">
          <fieldset class="ui-grid-a">
            <div>
              <button type="reset" data-theme="d">Reset</button>
            </div>
          </fieldset>
        </li>
      </ul>
    </form>
  </div>

最佳答案

通过parseFloat()将所有值转换为数字。
使用0作为默认值。
删除无用的eval()




$(document).ready(function(){
  $("#cal").click(function() {
    var a = parseFloat(document.getElementById('name1').value) || 0
    var b = parseFloat($('#grade1 option:selected').val()) || 0
    var c = parseFloat(document.getElementById('name2').value) || 0
    var d = parseFloat($('#grade2 option:selected').val()) || 0
    var e = parseFloat(document.getElementById('name3').value) || 0
    var f = parseFloat($('#grade3 option:selected').val()) || 0
    var g = parseFloat(document.getElementById('name4').value) || 0
    var h = parseFloat($('#grade4 option:selected').val()) || 0
    var i = parseFloat(document.getElementById('name5').value) || 0
    var j = parseFloat($('#grade5 option:selected').val()) || 0
    var m1 = a * b;
    var m2 = c * d;
    var m3 = e * f;
    var m4 = g * h;
    var m5 = i * j;
    var total1 = m1 + m2 + m3 + m4 + m5;
    var total2 = a + c + e + g + i;
    var total3 = total1/total2
    var total3 = total3.toFixed(2);

    $('#res').val(total3);
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div data-role="page">
  <div data-role="header" data-theme="f"> <a data-iconpos="notext" data-role="button" data-icon="home" title="Home">Home</a>
    <h1>BENG GPA Calculator</h1>
    <form id="form">
      <ul data-role="listview" data-inset="true">
        <li>Module 1</li>
        <li data-role="fieldcontain">
          <label for="name1">Credits:</label>
          <input type="text" name="name1" id="name1" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade1" class="select">Grade:</label>
          <select class="grade1" data-theme="f" id="grade1">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option
              >
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 2</li>
        <li data-role="fieldcontain">
          <label for="name2">Credits:</label>
          <input type="text" name="name2" id="name2" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade2" class="select">Grade:</label>
          <select class="grade2" data-theme="f" id="grade2">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 3</li>
        <li data-role="fieldcontain">
          <label for="name3">Credits:</label>
          <input type="text" name="name3" id="name3" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade3" class="select">Grade:</label>
          <select class="grade3" data-theme="f" id="grade3">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 4</li>
        <li data-role="fieldcontain">
          <label for="name4">Credits:</label>
          <input type="text" name="name4" id="name4" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade4" class="select">Grade:</label>
          <select class="grade4" data-theme="f" id="grade4">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li>Module 5</li>
        <li data-role="fieldcontain">
          <label for="name5">Credits:</label>
          <input type="text" name="name5" id="name5" value="" data-clear-btn="true">
        </li>
        <li data-role="fieldcontain">
          <label for="grade5" class="select">Grade:</label>
          <select class="grade5" data-theme="f" id="grade5">
            <option value="-1">—</option>
            <option value="4">A</option>
            <option value="3.7">A-</option>
            <option value="3.3">B+</option>
            <option value="3">B</option>
            <option value="2.7">B-</option>
            <option value="2.3">C+</option>
            <option value="2">C</option>
            <option value="1.7">C-</option>
            <option value="1.3">D+</option>
            <option value="1">D</option>
            <option value="0">F</option>
          </select>
        </li>
        <li data-role="fieldcontain">
          <label for="res">Result:</label>
          <input type="text" name="res" id="res" value="" data-clear-btn="true">
        </li>
        <li class="ui-body ui-body-b">
          <fieldset class="ui-grid-a">
            <div>
              <button type="button" id="cal" name="cal" data-theme="f">Calculate</button>
            </div>
          </fieldset>
        </li>
        <li class="ui-body ui-body-b">
          <fieldset class="ui-grid-a">
            <div>
              <button type="reset" data-theme="d">Reset</button>
            </div>
          </fieldset>
        </li>
      </ul>
    </form>
  </div>

关于javascript - 当我在Javascript中输入第二个模块时,结果错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33553531/

10-10 05:41