如何将表单输出的内容输出到页面中

时间:2015-04-21 22:15:04

标签: javascript jquery html forms element

我试图制作一个显示比赛获胜者的表格。 我已经能够获得胜利者的分数,但不是那个人的名字。 我用jquery calx来做数学计算,但只是说胜利者是谁让我疯了。我花了一天的大部分时间试图解决这个问题。

这是代码。

<!DOCTYPE HTML>

<html>

<head>
  <title>Untitled</title>

<script src="js/jquery-calx-master/js/numeral.min.js"></script>
  <script src="D:/Catapult Cruzers/web site/js/jquery-calx-master/jquery-1.9.1.min.js"></script>
  <script src="D:/Catapult Cruzers/web site/js/jquery-calx-master/jquery-calx-2.2.0.min.js"></script>
<script>
$(document).ready(function(){
     $('#sheet').calx();
});
</script>
</head>

<body>



                                       <form id="sheet">
Pilot <input data-cell="p1" name="Pilot1">
Run 1    <input   maxlength="5" size="5" type="text" data-cell="A1" />
Run 2    <input  maxlength="5" size="5"  type="text" data-cell="A2" />
Run 3    <input  maxlength="5" size="5" type="text" data-cell="A3" />
  Max  <input  maxlength="5" size="5" type="text" data-cell="A4" data-formula="max(A1:A3)" />
  Ave  <input  maxlength="5" size="5" type="text" data-cell="A5" data-formula="(sum(A1:A3))/3 " data-format="0.000" />
   <br>
  Pilot <input data-cell="p2" name="Pilot2">
Run 1    <input   maxlength="5" size="5" type="text" data-cell="B1" />
Run 2    <input  maxlength="5" size="5"  type="text" data-cell="B2" />
Run 3    <input  maxlength="5" size="5" type="text" data-cell="B3" />
  Max  <input  maxlength="5" size="5" type="text" data-cell="B4" data-formula="max(B1:B3)" />
  Ave  <input  maxlength="5" size="5" type="text" data-cell="B5" data-formula="(sum(B1:B3))/3 " data-format="0.000" />
    <br>
  Pilot <input data-cell="p3" name="Pilot3">
Run 1    <input   maxlength="5" size="5" type="text" data-cell="C1" />
Run 2    <input  maxlength="5" size="5"  type="text" data-cell="C2" />
Run 3    <input  maxlength="5" size="5" type="text" data-cell="C3" />
  Max  <input  maxlength="5" size="5" type="text" data-cell="C4" data-formula="max(C1:C3)" />
  Ave  <input  maxlength="5" size="5" type="text" data-cell="C5" data-formula="(sum(C1:C3))/3 " data-format="0.000" />
    <br>
  Pilot <input data-cell="p4" name="Pilot4">
Run 1    <input   maxlength="5" size="5" type="text" data-cell="D1" />
Run 2    <input  maxlength="5" size="5"  type="text" data-cell="D2" />
Run 3    <input  maxlength="5" size="5" type="text" data-cell="D3" />
  Max  <input  maxlength="5" size="5" type="text" data-cell="D4" data-formula="max(D1:D3)" />
  Ave  <input  maxlength="5" size="5" type="text" data-cell="D5" data-formula="(sum(D1:D3))/3 " data-format="0.000" />
<br><br>

WINNER   <input  maxlength="5" size="5" type="text" data-cell="w1" data-formula="MAX(A4:D4)" data-format="0.000" />



WINNER AVE  <input  maxlength="5" size="5" type="text" data-cell="w2" data-formula="MAX(A5:D5)" data-format="0.000" />

</form>




</body>

</html>

除了获胜者和获胜者平均分数(最高平均分数)之外,我还希望将飞行员的名字(p1,p2,p3,p4)与分数一起显示。类似的东西:

Winner Jimmy 8.52目前它只是Winner 8.52

1 个答案:

答案 0 :(得分:0)

...克里斯

我从来没有使用过calx框架,所以我想我会回答,尽管它几乎就像我为你做的一样。 calx的文档不是最好的,但是在GitHub颠覆中有一些样本。

确保您重命名&#39; Pilot&#39;数据单元格有一个大写字母P.除此之外,下面的代码应该可以正常工作。

如果您从这篇文章中删除任何内容,请至少考虑以下内容。

  • 有更清晰的代码,适当的缩进创造了一个更好的可读性的整个世界
  • 当你在Stack上发帖时,你应该确保阅读并重新阅读你写的所有内容。 (你所拥有的是相当混乱的)
  • 玩得开心,确保你宣布我回答这个问题!

    <!DOCTYPE HTML>
    
    <html>
    <head>
    
        <title>Untitled</title>
    
        <script src="js/jquery-calx-master/js/numeral.min.js"></script>
        <script src="D:/Catapult Cruzers/web site/js/jquery-calx-master/jquery-1.9.1.min.js"></script>
        <script src="D:/Catapult Cruzers/web site/js/jquery-calx-master/jquery-calx-2.2.0.min.js"></script>
    
    
    </head>
    
    <script>
    
        var max_user_cell = "D";
    
        $(document).ready(function(){
             $('#sheet').calx();
        });
    
        function getWinner()
        {
            var sheet = $('#sheet').calx('getSheet')
            var avg_array = new Array();
            var results = document.getElementById('results');
            var winner;
            var highest_avg;
    
            var count = 0;
            var cell_letter = "A";
            while(count < 4)
            {
                avg_array[count] = sheet.evaluate('(sum(' + cell_letter + '1:' + cell_letter + '3))/3');
                cell_letter = nextChar(cell_letter);
                count++;
            }
    
            highest_avg = Math.max.apply(Math, avg_array);
            winner = avg_array.indexOf(highest_avg);
            winner++;
    
            results.innerHTML = sheet.getCellValue('P' + winner) + " is the winner with a time of " + highest_avg.toFixed(2);
        }
    
    
        function nextChar(c)
        {
            return String.fromCharCode(c.charCodeAt(0) + 1);
        }
    
    </script>
    
    <body>
    
    
    <form id="sheet" onchange="getWinner()">
    
        Pilot <input data-cell="P1" name="Pilot1">
            Run 1    <input   maxlength="5" size="5" type="text" data-cell="A1" />
            Run 2    <input  maxlength="5" size="5"  type="text" data-cell="A2" />
            Run 3    <input  maxlength="5" size="5" type="text" data-cell="A3" />
            Max  <input  maxlength="5" size="5" type="text" data-cell="A4" data-formula="max(A1:A3)" />
            Ave  <input  maxlength="5" size="5" type="text" data-cell="A5" data-formula="(sum(A1:A3))/3 " data-format="0.000" />
    
        <br>
    
        Pilot <input data-cell="P2" name="Pilot2">
            Run 1    <input   maxlength="5" size="5" type="text" data-cell="B1" />
            Run 2    <input  maxlength="5" size="5"  type="text" data-cell="B2" />
            Run 3    <input  maxlength="5" size="5" type="text" data-cell="B3" />
            Max  <input  maxlength="5" size="5" type="text" data-cell="B4" data-formula="max(B1:B3)" />
            Ave  <input  maxlength="5" size="5" type="text" data-cell="B5" data-formula="(sum(B1:B3))/3 " data-format="0.000" />
    
        <br>
    
        Pilot <input data-cell="P3" name="Pilot3">
            Run 1    <input   maxlength="5" size="5" type="text" data-cell="C1" />
            Run 2    <input  maxlength="5" size="5"  type="text" data-cell="C2" />
            Run 3    <input  maxlength="5" size="5" type="text" data-cell="C3" />
            Max  <input  maxlength="5" size="5" type="text" data-cell="C4" data-formula="max(C1:C3)" />
            Ave  <input  maxlength="5" size="5" type="text" data-cell="C5" data-formula="(sum(C1:C3))/3 " data-format="0.000" />
    
        <br>
    
        Pilot <input data-cell="P4" name="Pilot4">
            Run 1    <input   maxlength="5" size="5" type="text" data-cell="D1" />
            Run 2    <input  maxlength="5" size="5"  type="text" data-cell="D2" />
            Run 3    <input  maxlength="5" size="5" type="text" data-cell="D3" />
            Max  <input  maxlength="5" size="5" type="text" data-cell="D4" data-formula="max(D1:D3)" />
            Ave  <input  maxlength="5" size="5" type="text" data-cell="D5" data-formula="(sum(D1:D3))/3 " data-format="0.000" />
    
        <br>
        <br>
    
        WINNER   <input  maxlength="5" size="5" type="text" data-cell="w1" data-formula="MAX(A4:D4)" data-format="0.000" />
    
        WINNER AVE  <input  maxlength="5" size="5" type="text" data-cell="w2" data-formula="MAX(A5:D5)" data-format="0.000" />
    
        <br>
        <br>
    
        <span id="results"></span>
    
    </form>
    
    
    </body>
    
    </html>