我做了一个简单的 GPA(平均绩点)计算器,我只是在玩一些东西,真的很想知道如何允许用户随意添加行。我能找到的最好的例子是在这个网站上:http: //gpacalculator.net/high-school-gpa-calculator/
我想我可能会理解当用户单击按钮时,jquery 函数可以添加一行,但我如何获得它,所以每次有人添加一行时输入元素的名称都是不同的。php 文件如何知道 $_POST['someName'] 的名称。
所以简而言之:如何使用 jquery 添加一行,然后将该表单提交到 php 文件,使其与预制行不同。
以防万一您需要知道,我将发送到 php 文件的值存储在变量中。然后将它们输入到计算 GPA 的公式中。
编辑这里是表代码:
<table>
<form id = "myform" name = "myform" method = "POST" action = "">
<tr>
<th> Grade in test </th>
<th> Weight of grade </th> <br />
</tr>
<tr> <td><input type = "text" name = "grade1" /> </td> <td> <input type = "text" name = "weight1" /> </td> </tr>
<tr> <td><input type = "text" name = "grade2" /> </td> <td> <input type = "text" name = "weight2" /> </td> </tr>
<tr> <td><input type = "text" name = "grade3" /> </td> <td> <input type = "text" name = "weight3" /> </td> </tr>
<tr> <td><input type = "text" name = "grade4" /> </td> <td> <input type = "text" name = "weight4" /> </td> </tr>
<tr> <td><input type = "text" name = "grade5" /> </td> <td> <input type = "text" name = "weight5" /> </td> </tr>
<tr> <td><input type = "text" name = "grade6" /> </td> <td> <input type = "text" name = "weight6" /> </td> </tr>
<tr> <td><input type = "text" name = "grade7" /> </td> <td> <input type = "text" name = "weight7" /> </td> </tr>
<tr> <td><input type = "text" name = "grade8" /> </td> <td> <input type = "text" name = "weight8" /> </td> </tr>
<tr> <td><input type = "text" name = "grade9" /> </td> <td> <input type = "text" name = "weight9" /> </td> </tr>
<tr> <td><input type = "text" name = "grade10" /> </td> <td> <input type = "text" name = "weight10" /> </td> </tr>
<tr> <td><input type = "text" name = "grade11" /> </td> <td> <input type = "text" name = "weight11" /> </td> </tr>
<tr> <td><input type = "text" name = "grade12" /> </td> <td> <input type = "text" name = "weight12" /> </td> </tr>
<tr>
<td> <div id = "submitButton">
<input type = "submit" value = "SUBMIT" style = " font-family:'HelveticaNeue-Light', 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; padding: 10px;border: none; color: white; background-color: #0499ff; width: 100%; font-size: 13px; border-radius: 5px;""/></div></td> <td> <input type = "reset" /> </td></tr>
</form>
</table>
<div id = "results2"></div>
</div>
然后将 PHP 文件中的代码缩减为初始变量:
/* GRADES VARS */
$grade1 = $_POST['grade1'];
$grade2 = $_POST['grade2'];
$grade3 = $_POST['grade3'];
$grade4 = $_POST['grade4'];
$grade5 = $_POST['grade5'];
$grade6 = $_POST['grade6'];
$grade7 = $_POST['grade7'];
$grade8 = $_POST['grade8'];
$grade9 = $_POST['grade9'];
$grade10 = $_POST['grade10'];
$grade11 = $_POST['grade11'];
$grade12 = $_POST['grade12'];
/* GRADE WEIGHT VARS */
$weight1 = $_POST['weight1'];
$weight2 = $_POST['weight2'];
$weight3 = $_POST['weight3'];
$weight4 = $_POST['weight4'];
$weight5 = $_POST['weight5'];
$weight6 = $_POST['weight6'];
$weight7 = $_POST['weight7'];
$weight8 = $_POST['weight8'];
$weight9 = $_POST['weight9'];
$weight10 = $_POST['weight10'];
$weight11 = $_POST['weight11'];
$weight12 = $_POST['weight12'];
谢谢!