在我的應用我有一個表,它看起來像下面這樣的例子:如何執行計算函數來計算差異?
Question No. Question Answer Marks per Answer Total Marks
1 Here are 2 answers B (text input) 5
D (text input)
2 Here is a single answer True (text input) 5
在上表中我有屬於每個答案的文本輸入。現在每個問題都有自己的總分數。我想要做的是,如果用戶在文本輸入中輸入一個數字,它應該計算輸入到文本輸入中的數字與「總標記」列下的數字之間的差異。所以,如果你看看下面的例子:
Question No. Question Answer Marks per Answer Total Marks
1 Here are 2 answers B (text input) = 2 2
D (text input) = 1
2 Here is a single answer True (text input) = 5 0
正如你可以在上面的表格中看到,在問題1的答案的文字輸入完全等於3。因此,5(從問題1的總分數)減3 = 2(總分數現在等於2)
對於問題2,問題2的答案的文本輸入等於5,所以5(從問題2的總分數)減5 = 0(現在總分數等於0)。
我的問題:
- 如何,哪些是爲了用來執行這些計算的最佳方法是什麼?
2.我還想要的是,如果一個問題只包含一個答案(就像上面例子中的問題2只有一個答案),那麼我希望文本輸入變爲只讀,文本輸入應該顯示相同數量佔總分的,它有反正等於用相同的標記,所以它看起來像下面這樣:
Question No. Question Answer Marks per Answer Total Marks
2 Here is a single answer True (text input(readonly)) = 5 0
這裏是一個的jsfiddle demo讓你知道的HTML代碼是什麼樣子。在jsfidle中,我所做的是在文本框中插入了值,但是如果計算總計之間的差值,我將「總分」留給「5」,當他們都應該使「0」標記和文本輸入中輸入的數字。還質疑2的文字輸入是隻讀的,這是因爲它只是一個單一的答案,這個問題
下面是HTML代碼,這是在的jsfiddle:
<table border='1' id='markstbl'>
<tr>
<th class='questionth'>Question No.</th>
<th class='questionth'>Question</th>
<th class='answerth'>Answer</th>
<th class='answermarksth'>Marks per Answer</th>
<th class='noofmarksth'>Total Marks</th>
</tr>
<tr class="questiontd">
<td class="questionnumtd" rowspan="2">1</td>
<td class="questioncontenttd" rowspan="2">Here are 2 Answers Question</td>
<td class="answertd">B</td>
<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" value="3" /></td>
<td class="noofmarkstd" rowspan="2">5</td>
</tr>
<tr class="questiontd">
<td class="answertd">D</td>
<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" value="2" /></td>
</tr>
<tr class="questiontd">
<td class="questionnumtd" rowspan="2">2</td>
<td class="questioncontenttd" rowspan="2">Here is single Answer Question</td>
<td class="answertd">True</td>
<td class="answermarkstd"><input class="individualMarks" name="answerMarks[]" id="individualtext" type="text" value="5" readonly="readonly"/></td>
<td class="noofmarkstd" rowspan="2">5</td>
</tr>
</table>
我只想說,有可無數的問題和衆多的答案,請不要只提供一個只適合這個例子的答案。如果問題中有很多答案,它需要工作。
你爲什麼要發佈的服務器代碼,而不是住HTML?你可以更容易地使用html和jsfiddle.net中的演示來描述roww規則 – charlietfl
@charlietfl當你說你想看到實時html,你的意思是查看頁面源代碼嗎?我非常樂意創建一個jsfiddle演示來顯示可能的表格。給我10分鐘,在jsfiddle – user1794342
是的..取代所有的PHP ...它不適合在JavaScript計算問題的上下文演示。 – charlietfl