简体   繁体   English

在使用 jQuery 在特定结果 div 中单击它后,如何显示所有动态无线电输入值的总和?

[英]How do I show the sum of all dynamic radio input values after I click on it in a specific result div by using jQuery?

This is my last post here in this coder's community because I ask this in many ways but didn't get the proper solution.这是我在这个编码器社区中的最后一篇文章,因为我在很多方面都提出了这个问题,但没有得到正确的解决方案。 So I simplify my code and paste it here again.所以我简化了我的代码并再次粘贴到这里。 I'm not so good in jquery/js so that is why I'm stuck with this code.我在 jquery/js 方面不太好,所以这就是我坚持使用这段代码的原因。

Please help me, It'll be great help for me because this is my first project and I just moved from a banking sector to programming world.请帮助我,这对我很有帮助,因为这是我的第一个项目,我刚从银行业转到编程世界。 Thanks in advance for all your efforts and help.提前感谢您的所有努力和帮助。

Following is the code where I mentioned the static version of html part(it is dynamically coming from database).以下是我提到 html 部分的 static 版本的代码(它是动态来自数据库的)。

 var matched = $('.option_yes'); var countedItem = matched.length; //This code is for the example purpose to show the value when hit on radio button $(document).on('click', '.extraRoomOption:checked', function(e){ //$('#p'+i+'_price').html($(this).val()); alert($(this).val()); }); //This is the rubished(Because I wrote this) code that will be use in production: var i; for(i = 1; i <= countedItem; i++){ $('input[name=extraRoom'+i+']').on('click', function(e){ if($(e.target).is('#extraRoomInput_y'+i)){ $('#p'+i+'_price').html($(this).val()); } }); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h3>Maggie's room type</h3> <p>would you like your own room?</p> <label> <input type="radio" class="extraRoomOption option_yes" name="extraRoom1" id="y1_extraRoomInput" value="150000"> <b>Yes, please</b> </label> <label> <input type="radio" class="extraRoomOption option_no" name="extraRoom1" id="n1_extraRoomInput" value="125000" checked> <b>No, thanks</b> </label> <hr> <h3>Esther's room type</h3> <p>would you like your own room?</p> <label> <input type="radio" class="extraRoomOption option_yes" name="extraRoom2" id="y2_extraRoomInput" value="150000"> <b>Yes, please</b> </label> <label> <input type="radio" class="extraRoomOption option_no" name="extraRoom2" id="n2_extraRoomInput" value="125000" checked> <b>No, thanks</b> </label> <br> <hr><hr> <h2>Room type</h2> <p>Maggie (Twin Room) And Price is <span id="p1_price">125000</span></p> <p>Esther (Twin Room) And Price is <span id="p2_price">125000</span></p> <p><b>Total Price is <span id="totalPrice">250000</span></b></p>

Ignore this if you don't want to know about how my website is working.如果您不想知道我的网站是如何运作的,请忽略这一点。

Let me explain first how my website should be work:让我先解释一下我的网站应该如何工作:
1) When a user come to my website and click on tour and then fill booking form with his basic personal information. 1) 当用户来到我的网站并点击游览,然后在预订表格中填写他的基本个人信息。
2) After that all information will be save into the database and then the tailor your trip page appear just after the first step. 2) 之后,所有信息将被保存到数据库中,然后在第一步之后出现量身定制的旅行页面
3) Now this is the tricky part where I'm stuck. 3)现在这是我卡住的棘手部分。 When user click on Room type option it should work like: The price of the selected passenger would be update automatically and then sum of all passenger's price should be show in final price box.当用户点击房间类型选项时,它应该像这样工作:所选乘客的价格将自动更新,然后所有乘客价格的总和应显示在最终价格框中。
4) Here are the my previous question link: 4)这是我之前的问题链接:

Show sum of the dynamic price when click on radio button in jquery 单击 jquery 中的单选按钮时显示动态价格的总和
Show sum of dynamic radio input element and It should also show when click on another radio input element 显示动态单选输入元素的总和,并在单击另一个单选输入元素时也应显示

5) See the picture below: 5)见下图: 我的网站的工作流程

You need to add the form tag (It is easier to work with input fields with it).您需要添加表单标签(使用它更容易处理输入字段)。 Please note the html code is almost the same as from your example, only the form tag was added.请注意 html 代码与您的示例几乎相同,只是添加了表单标签。

 document.addEventListener('DOMContentLoaded', function() { var form = document.getElementById('radio-form'); var inputs = form.querySelectorAll('input[type="radio"]'); var p1 = document.getElementById('p1_price'); var p2 = document.getElementById('p2_price'); var total = document.getElementById('totalPrice'); function setPrice() { var room1 = form.elements['extraRoom1']; var room2 = form.elements['extraRoom2']; p1.innerHTML = room1.value; p2.innerHTML = room2.value; total.innerHTML = +room1.value + +room2.value; }; Array.prototype.forEach.call(inputs, function(input) { input.addEventListener('change', setPrice); }); setPrice(); });
 <h3>Maggie's room type</h3> <p>would you like your own room?</p> <form id="radio-form"> <label> <input type="radio" class="extraRoomOption option_yes" name="extraRoom1" id="y1_extraRoomInput" value="150000"> <b>Yes, please</b> </label> <label> <input type="radio" class="extraRoomOption option_no" name="extraRoom1" id="n1_extraRoomInput" value="125000" checked> <b>No, thanks</b> </label> <hr> <h3>Esther's room type</h3> <p>would you like your own room?</p> <label> <input type="radio" class="extraRoomOption option_yes" name="extraRoom2" id="y2_extraRoomInput" value="150000"> <b>Yes, please</b> </label> <label> <input type="radio" class="extraRoomOption option_no" name="extraRoom2" id="n2_extraRoomInput" value="125000" checked> <b>No, thanks</b> </label> </form> <br> <hr> <hr> <h2>Room type</h2> <p>Maggie (Twin Room) And Price is <span id="p1_price">125000</span></p> <p>Esther (Twin Room) And Price is <span id="p2_price">125000</span></p> <p><b>Total Price is <span id="totalPrice">250000</span></b></p>

Correct the code if necessary.必要时更正代码。 I hope I helped you.我希望我对你有所帮助。

This code might be helpful... I have added data-target attribute to target particular price with ID.此代码可能会有所帮助...我添加了data-target属性以使用 ID 定位特定价格。

 jQuery(document).ready(function($) { $(document).on('click', '.extraRoomOption:checked', function(e){ var targetPrice = $(this).data('target'); $('#'+targetPrice).text($(this).val()); grandTotal(); }); function grandTotal(){ var total = 0; $('.extraRoomOption:checked').each(function(e){ total += parseFloat($(this).val()); }); $('#totalPrice').text(total); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h3>Maggie's room type</h3> <p>would you like your own room?</p> <label> <input type="radio" class="extraRoomOption option_yes" name="extraRoom1" id="y1_extraRoomInput" data-target="p1_price" value="150000"> <b>Yes, please</b> </label> <label> <input type="radio" class="extraRoomOption option_no" name="extraRoom1" id="n1_extraRoomInput" value="125000" data-target="p1_price" checked> <b>No, thanks</b> </label> <hr> <h3>Esther's room type</h3> <p>would you like your own room?</p> <label> <input type="radio" class="extraRoomOption option_yes" name="extraRoom2" id="y2_extraRoomInput" data-target="p2_price" value="150000"> <b>Yes, please</b> </label> <label> <input type="radio" class="extraRoomOption option_no" name="extraRoom2" id="n2_extraRoomInput" value="125000" data-target="p2_price" checked> <b>No, thanks</b> </label> <br> <hr><hr> <h2>Room type</h2> <p>Maggie (Twin Room) And Price is <span id="p1_price">125000</span></p> <p>Esther (Twin Room) And Price is <span id="p2_price">125000</span></p> <p><b>Total Price is <span id="totalPrice">250000</span></b></p>

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

相关问题 如何使用jquery点击功能后刷新div? - how do i refresh the div after a click function using jquery? 如何使用jQuery计算单选按钮值的总和? - How might I calculate the sum of radio button values using jQuery? 如果使用jquery选中了单选按钮,如何显示输入字段 - How do I show an input field if a radio button is checked using jquery jQuery的; 如何显示点击div并隐藏所有其他 - jquery; how do I show the click-on div and hide all the others 单击按钮后如何将具有多个类的数据属性值输入到div? - How do I input data attribute values with multiple classes to a div after button click? 如何在jquery中再次单击时显示DIV? - How do I show DIV when click again in jquery? 如何使用JQuery检查输入单选按钮? - How do I make an input radio button checked…using JQuery? 如何获取2个数字输入字段的值,然后使用Jquery进行除法并将结果显示给另一个输入? - How do i take values of 2 input fields which are numbers, then divide using Jquery and display the result to another input? 显示动态单选输入元素的总和,并在单击另一个单选输入元素时也应显示 - Show sum of dynamic radio input element and It should also show when click on another radio input element 如何使用Selenium Webdriver单击具有特定文本的div? - How do I click a div with a specific text using Selenium Webdriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM