简体   繁体   English

计算表单输入,其中一个由选择框修改

[英]Calculate form inputs, one modified by select box

I need to calculate based on user input on the form below. 我需要根据下面表格上的用户输入进行计算。 While using the jQuery Calculation Plug-in here: http://www.pengoworks.com/workshop/jquery/calculation/calculation.plugin.htm I was able to figure out how to do "How many sessions?" 在这里使用jQuery Calculation插件时: http : //www.pengoworks.com/workshop/jquery/calculation/calculation.plugin.htm我能够弄清楚如何进行“多少个会话?” times "Number of Students" times "Cost Per Session" for the top two items. 前两个项目乘以“学生人数”乘以“每次会话费用”。 The one that is really stumping me is the bottom one where in addition to the user filling in number of sessions I need them to select the number of students from a select box as well. 真正让我感到困扰的是底部的那个,除了用户填写的会话数外,我还需要他们从选择框中选择学生数。 Any ideas? 有任何想法吗?

<table>
<tr><th>How many sessions?</th><th>Number of Students</th><th>Type of tutoring</th><th>Cost Per Session</th><th>Total Cost</th></tr>
<tr><td><input type="text" name="qty_academic" size="3" /></td><td id="num_students">1 Student</td><td>Academic Tutoring</td><td id="price_item_1">$55.00</td><td id="total_item_1">$0.00</td></tr>
<tr><td><input type="text" name="qty_multi" size="3" /></td><td id="num_students">1 Student</td><td>ML Tutoring</td><td id="price_item_2">$60.00</td><td id="total_item_2">$0.00</td></tr>
<tr><td><input type="text" name="qty_group" size="3" /></td><td><select name="group_size" id="num_students">
    <option value=""></option>
    <option value="2">2 Students</option>
    <option value="3">3 Students</option>
    <option value="4">4 Students</option>
    </select></td><td>Group Tutoring</td><td id="price_item_3"></td><td id="total_item_3">$0.00</td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td colspan="2" align="right"><h3>Grand Total:</h3></td><td id="grandTotal"></td></tr>

The 2 student price would be $40 per student, 3 students = $35 per student, 4 students = $30 per student. 2位学生的价格为每位学生$ 40,3位学生=每位学生$ 35,4位学生=每位学生$ 30。 Maybe I'm just not looking at this the right way but I can't seem to wrap my head around it. 也许我只是没有以正确的方式看待它,但似乎无法将自己的头缠住。

In addition to doing the calculations I also need to make at it a requirement of form submission that at least one session in one of those types be selected (as well as all the information fields I'm using on the rest of the form). 除了进行计算外,我还需要对表单进行提交,要求至少选择一种类型的会话(以及我在其余表单上使用的所有信息字段)。 Can someone point me to a good example of how to accomplish that? 有人可以为我指出如何实现这一目标的好例子吗? Thanks. 谢谢。


UPDATE #1: 更新#1:

Here's what I have working so far: http://jsfiddle.net/jT9HE/2/ 到目前为止,这是我的工作: http : //jsfiddle.net/jT9HE/2/

I just need to make it recalculate when the drop-down (select) box is changed. 当下拉(选择)框更改时,我只需要使其重新计算即可。


UPDATE #2: 更新#2:

        $("select[name^=group_size]").bind("change", recalc);
        // run the calculation function now
        recalc();

This makes changes to the drop down box update the price. 这将使下拉框的更改更新价格。 Now how can I determine the price based on what is selected in the drop down box? 现在如何根据下拉框中选择的价格确定价格?

In my opinion this is a case where jQuery should complain. 我认为这是jQuery应该抱怨的情况。 Anyway, actually a source of problems is the presence of multiple elements with the same value for the attribute id . 无论如何,实际上问题的根源是存在多个具有相同属性id值的元素。 This is a code excerpt showing the issue: 这是显示此问题的代码摘录:

<td id="num_students">1 Student</td>
<td id="num_students">1 Student</td>

<select name="group_size" id="num_students">

When you do this, 当您这样做时

students: $("[id^=num_students]")

the resulting element is not what you would like but the first TD. 结果元素不是您想要的,而是第一个TD。

Hope this help for further investigation and code refactor. 希望这有助于进一步调查和代码重构。

-- UPDATE -- -更新-

I fixed your code, please take a look here on jsFiddle and see if it works for you. 我已经修复了您的代码,请在jsFiddle上查看一下,看看它是否对您有用 If not let me know. 如果没有让我知道。

Basically I solved the issue of multiple id with the same value and added hidden fields in order to always have a number of students to multiply. 基本上,我解决了具有相同值的多个id的问题,并添加了隐藏字段,以便始终有大量学生要乘。 Finally I attached an onchange event to the select box. 最后,我将一个onchange事件附加到选择框。

There is also a bit of logic for dynamic calculation of prices for Group tutoring. 动态计算团体补习价格也有一些逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算表单输入onChange - Calculate form inputs onChange jQuery 从 select 框中预填充表单字段并计算结果 - jQuery prepopulate form fields from select box and calculate results 使用 onekeyup 脚本计算表单输入 - Calculate form inputs with onekeyup script Atleast使用angularjs在表单验证中选中一个复选框 - Atleast select one check box in form validation using angularjs 如何通过在jquery中乘以一个选择框值和一个文本框值onchange来计算数量 - how to calculate the amount by multiplying one select box value & one textbox value onchange in jquery 抓取表单字段并根据输入进行计算 - Grab form fields and calculate based on inputs 如何基于其他表单框输入使用JavaScript填充表单框 - How to populate a form box with javascript based on other form box inputs 两种形式,一个选择/下拉框-需要将选择值从一种形式复制到另一种形式 - Two forms, one select/dropdown box - need to copy select value from one form to other 附加到两个输入的jQuery Datepicker,在修改一个输入时会同时更改 - jQuery Datepicker attached to two inputs, changes both when one is modified Bootstrap验证程序和图像选择器-如果选择了两个可用选择输入中的任何一个,如何验证表单? - Bootstrap Validator & Image Picker - How to validate form if any one of two available select inputs are chosen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM