复选框将输入添加到文本框总选择和总选择成本(Check box add input to text box total selection and total cost of selection)

编程入门 行业动态 更新时间:2024-10-25 19:36:44
复选框将输入添加到文本框总选择和总选择成本(Check box add input to text box total selection and total cost of selection) <script> var itemsAdded = Array(); function moveNumbers(text) { var i = itemsAdded.indexOf(text) if ( i >= 0) { itemsAdded.splice(i,1); } else { itemsAdded.push(text); } document.getElementById("result").value=itemsAdded.join(" "); } $(function() { for (i=0;i<10;i++) { console.log(i); $("body").append("<input type='checkbox' name='add' value='" + i + "' onclick='moveNumbers(this.value)'/> Checkbox" + i + "<br/>"); } }); </script> <textarea id="result" cols="12" rows="6" readonly> </textarea> <tr> <?php $path = "photos/"; $dir_handle = @opendir($path) or die("Unable to open folder"); echo "<table height='500px'width='800px'align='center'border='1'>"; echo "<tr>"; while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; { echo ($x % 6 == 0) ? "</tr><tr>" : ""; echo "<td><input type='checkbox' name='add' value='$file' onclick='moveNumbers(this.value)'> <img src='photos/$file'alt='$file' style='height:auto;width:85%;'alt='$file'> <br> $file </td>"; $x++; } } echo "</tr>"; echo "</table>"; closedir($dir_handle); ?>

大家好,我已经设法弄清楚如何将复选框输入添加到文本框以列出所选项目。感谢那些帮助我的人(弗雷德和乔)。 现在我需要添加另一个文本框来累计选择的项目总数,另外一个文本框添加所选项目的总成本。 EG Check box checked -> Add to list text box (this bit already works) -> Add total items listed to text box -> Add total cost of all items selected (each image costs say $10 each)我已经去了但是脚本是如此凌乱我以为我会发布原始的开始。 要问你想多少? 干杯。

<script> var itemsAdded = Array(); function moveNumbers(text) { var i = itemsAdded.indexOf(text) if ( i >= 0) { itemsAdded.splice(i,1); } else { itemsAdded.push(text); } document.getElementById("result").value=itemsAdded.join(" "); } $(function() { for (i=0;i<10;i++) { console.log(i); $("body").append("<input type='checkbox' name='add' value='" + i + "' onclick='moveNumbers(this.value)'/> Checkbox" + i + "<br/>"); } }); </script> <textarea id="result" cols="12" rows="6" readonly> </textarea> <tr> <?php $path = "photos/"; $dir_handle = @opendir($path) or die("Unable to open folder"); echo "<table height='500px'width='800px'align='center'border='1'>"; echo "<tr>"; while (false !== ($file = readdir($dir_handle))) { if($file == "index.php") continue; if($file == ".") continue; if($file == "..") continue; { echo ($x % 6 == 0) ? "</tr><tr>" : ""; echo "<td><input type='checkbox' name='add' value='$file' onclick='moveNumbers(this.value)'> <img src='photos/$file'alt='$file' style='height:auto;width:85%;'alt='$file'> <br> $file </td>"; $x++; } } echo "</tr>"; echo "</table>"; closedir($dir_handle); ?>

Hi all, I have managed to figure out how to add check box input to text box to list items selected.Thanks to those who help me on here (Fred and Joe). Now I need to add another text box to add up how many total items selected and another one to add total cost of all items selected. E.G. Check box checked -> Add to list text box (this bit already works) -> Add total items listed to text box -> Add total cost of all items selected (each image costs say $10 each) I have had a go but the script was so messy I thought I would just post the original starting one. To much to ask you think? Cheers.

最满意答案

我认为这会做你所要求的所有事情(注意我也已经清理了很多代码,我鼓励你查看更多有关jQuery的信息,因为你好像用JavaScript做了一些事情你也可以这样做用jQuery。

每次单击复选框时,它都会运行函数calculateCheckout() ,这会找到所有选中的复选框,并计算所选的总数和总价。 我将价格存储在名为data-price的属性中。 在我的JSFiddle中,它只需要增量值并将其乘以2,但在一个工作示例中,它应该是任何数字。 现在,解决方案将通过类product-check整个机构是否有任何输入。 只要你的复选框可以有这个类,那么这个解决方案就可以了。 我添加了一个示例产品,让您了解如何正确使用复选框。

的jsfiddle

JS

var itemsAdded = []; function calculateCheckout() { var checked_boxes = $("input.product-check:checked"); // Find all checked checkboxes var items_total = 0; $("#x-names").val(""); // Reset the list of names checked_boxes.each(function() { // Loop through all of our checkboxes // Add the name (taken from the checkbox data-name attribute) to the list of names $("#x-names").val($("#x-names").val() + " " + $(this).attr("data-name")); items_total += parseFloat($(this).val()); // Iterate our total price by taking the checkbox value }); // Record the total checkbox length $("#x-length").val(checked_boxes.length); $("#x-total").val(items_total); } $(document).ready(function() { // Set an event to call calculateCheckout every time an element with class .check-move is clicked. // See the jQuery 'on' function for more info $("body").on("click", "input.product-check", calculateCheckout); for (i=1;i<5;i++) { $("#checkboxes").append("<input type='checkbox' class='product-check' value='"+(i * 2 )+"' data-name='"+i+"' /> Checkbox" + i + "<br />"); } });

HTML

<div id="checkboxes"></div> <h1>Selected Products</h1> <textarea id="x-names"></textarea> <h1>Total Selected</h1> <input id="x-length" /> <h1>Total Price</h1> <input id="x-total" /> <input type="checkbox" value="745" data-name="samgs4" class="product-check" /> Galaxy S4

I think this does everything that you've asked for (note I've also cleaned the code up a lot, I'd encourage you to check out more about jQuery as you seemed to be doing things with JavaScript that you might as well do with jQuery.

Every time you click a checkbox, it runs the function calculateCheckout(), this finds all of the checked checkboxed, and calculates the total number selected, and the total price. I'm storing the price in an attribute called data-price. In my JSFiddle it just takes the incremental value and multiplies it by two, but in a working example it should be anything numeric. The solution will now check the entire body for any input with the class product-check. As long as your checkboxes can have this class then this solution will work. I've added an example product to give you an idea of how you should use the checkboxes correctly.

JSFiddle

JS

var itemsAdded = []; function calculateCheckout() { var checked_boxes = $("input.product-check:checked"); // Find all checked checkboxes var items_total = 0; $("#x-names").val(""); // Reset the list of names checked_boxes.each(function() { // Loop through all of our checkboxes // Add the name (taken from the checkbox data-name attribute) to the list of names $("#x-names").val($("#x-names").val() + " " + $(this).attr("data-name")); items_total += parseFloat($(this).val()); // Iterate our total price by taking the checkbox value }); // Record the total checkbox length $("#x-length").val(checked_boxes.length); $("#x-total").val(items_total); } $(document).ready(function() { // Set an event to call calculateCheckout every time an element with class .check-move is clicked. // See the jQuery 'on' function for more info $("body").on("click", "input.product-check", calculateCheckout); for (i=1;i<5;i++) { $("#checkboxes").append("<input type='checkbox' class='product-check' value='"+(i * 2 )+"' data-name='"+i+"' /> Checkbox" + i + "<br />"); } });

HTML

<div id="checkboxes"></div> <h1>Selected Products</h1> <textarea id="x-names"></textarea> <h1>Total Selected</h1> <input id="x-length" /> <h1>Total Price</h1> <input id="x-total" /> <input type="checkbox" value="745" data-name="samgs4" class="product-check" /> Galaxy S4

更多推荐

echo,text,电脑培训,计算机培训,IT培训"/> <meta name="description" c

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

发布评论

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

>www.elefans.com

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