如果有大量文本输入,如何执行计算(How to perform calculation if there a numerous text inputs)

编程入门 行业动态 更新时间:2024-10-25 16:24:33
如果有大量文本输入,如何执行计算(How to perform calculation if there a numerous text inputs)

我想对用户在问题中每个答案的文本输入中输入的数字与问题的总分数之间的差异进行计算。

我有一个jsfiddle应用程序,目前正在执行此操作。 当您在答案的文本输入中更改值时,它将计算输入的数字与属于该问题的总标记之间的差异。

我的问题是,目前只有我有一个或两个答案才有效。 但是一个问题可以有很多答案,他们可以有3个,4个,5个甚至10个答案甚至更多。 所以我的问题是,如何改变下面的小提琴,以便它可以执行多个文本框的计算,而不仅仅是2个文本框?

http://jsfiddle.net/jTXy5/3/这个jsfiddle有一个包含3个答案的问题。 但第三个文本输入无法计算

以下是应该发生的事情的示例:

Question No. Question Answer Marks per Answer Total Marks 1 Here are 2 answers B (text input) = 2 1 D (text input) = 1 E (text input) = 1 2 Here is a single answer True (text input) = 5 0

正如您在上表中所看到的,问题1中答案的文本输入完全等于4。 所以5(从问题1的总分数)减去4 = 1(总分数现在等于1)

对于问题2,问题2中答案的文本输入等于5,因此5(来自问题2的总分数)减去5 = 0(总分数现在等于0)。

下面是执行计算的代码:

$('tr').each(function() { var $input = $(this).find('input'); var $row = $(this); var is_multiple = !$input.prop('readonly'); var rowClass = is_multiple ? 'multiple' : 'single'; if (is_multiple) { var is_first = $row.find('td').length == 5; rowClass += is_first ? ' first' : ' second'; } else { /* readonly just needs marks changed once on page load */ $row.find('.noofmarkstd').text(5 - $input.val()); } $input.addClass(rowClass); }); $('input.multiple').keyup(function() { var $input = $(this); var is_first = $input.is('.first'); var $row = $input.closest('tr'); var $otherRow = $row[is_first ? 'next' : 'prev'](); var $marks = is_first ? $row.find('.noofmarkstd') : $otherRow.find('.noofmarkstd'); var calcs = 5 - ($input.val() || 0) - ( $otherRow.find('input.multiple').val() || 0); $marks.text(calcs); }); /* if need calcs for multiples generated on pageload trigger a change on the first in set*/ $('input.first').change();

I want to perform a calculation on the difference between the number the user has entered in a text input for each answer in a question, and the total marks for the question.

I have a jsfiddle application which currently do this. When you change a value with in an answer's text input, it will calculate the difference between that number entered and the total marks which belongs to that question.

My problem is that at the moment it only works if I have either one or 2 answers. But a question can have numerous answers, they can have 3, 4, 5 even 10 answers and more. So my question is that how can the fiddle below can be changed so that it can perform calculations for multiple textboxes, not just 2 text boxes?

http://jsfiddle.net/jTXy5/3/ This jsfiddle has a question which contains 3 answers. But the third text input doesn't get calculated

Below is the example of what should happen:

Question No. Question Answer Marks per Answer Total Marks 1 Here are 2 answers B (text input) = 2 1 D (text input) = 1 E (text input) = 1 2 Here is a single answer True (text input) = 5 0

As you can see in the table above, the text inputs for the answers in question 1 equals 4 altogether. So 5 (from total marks for question 1) minus 4 = 1 (Total marks now equals 1)

For question 2, the text input for the answer in question 2 equals 5, so 5 (from total marks for question 2) minus 5 = 0 (Total marks now equals 0).

Below is the code which performs the calculation:

$('tr').each(function() { var $input = $(this).find('input'); var $row = $(this); var is_multiple = !$input.prop('readonly'); var rowClass = is_multiple ? 'multiple' : 'single'; if (is_multiple) { var is_first = $row.find('td').length == 5; rowClass += is_first ? ' first' : ' second'; } else { /* readonly just needs marks changed once on page load */ $row.find('.noofmarkstd').text(5 - $input.val()); } $input.addClass(rowClass); }); $('input.multiple').keyup(function() { var $input = $(this); var is_first = $input.is('.first'); var $row = $input.closest('tr'); var $otherRow = $row[is_first ? 'next' : 'prev'](); var $marks = is_first ? $row.find('.noofmarkstd') : $otherRow.find('.noofmarkstd'); var calcs = 5 - ($input.val() || 0) - ( $otherRow.find('input.multiple').val() || 0); $marks.text(calcs); }); /* if need calcs for multiples generated on pageload trigger a change on the first in set*/ $('input.first').change();

最满意答案

var $inputs = $('input.individualMarks'); $inputs.filter(function() { return $(this).prop('readonly') === true; }).each(function() { var $input = $(this); var $marks = $input.closest('tr').css('color', 'red').find('td.noofmarkstd'); $marks.html(5 - ($input.val() || 0) + '<br>(single test)'); }); $inputs.filter('[data-q_group]').keyup(function() { var $group = $inputs.filter('[data-q_group="' + $(this).data('q_group') + '"]'); var $marks = $group.eq(0).closest('tr').find('td.noofmarkstd'); var markVal = 5; $group.each(function() { markVal -= ($(this).val() || 0) }) $marks.text(markVal) })​ var $inputs = $('input.individualMarks'); $inputs.filter(function() { return $(this).prop('readonly') === true; }).each(function() { var $input = $(this); var $marks = $input.closest('tr').css('color', 'red').find('td.noofmarkstd'); $marks.html(5 - ($input.val() || 0) + '<br>(single test)'); }); $inputs.filter('[data-q_group]').keyup(function() { var $group = $inputs.filter('[data-q_group="' + $(this).data('q_group') + '"]'); var $marks = $group.eq(0).closest('tr').find('td.noofmarkstd'); var markVal = 5; $group.each(function() { markVal -= ($(this).val() || 0) }) $marks.text(markVal) })​

更多推荐

本文发布于:2023-08-06 15:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450208.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   perform   calculation   inputs   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!