简体   繁体   English

如果选中复选框,我如何从复选框中获取属性值?

[英]How can i get attribute value from a checkbox if it's checked?

Well, I have this text input that gets sum of all the attributes of "price" inside all checkboxes are checked. 好吧,我有这个文本输入,检查所有复选框内的“价格”的所有属性的总和。 now i can't get an attribute value from a checkbox. 现在我无法从复选框中获取属性值。

here is my function: 这是我的功能:

function sum_options() {
    var options = [];
    $("#form-field-1-11-1").attr("price", 500);
    $("#form-field-1-11-2").attr("price", 500);
    $("#form-field-1-11-3").attr("price", 0);
    $("#form-field-1-11-4").attr("price", 300);
    $("#form-field-1-11-5").attr("price", 500);
    $("#form-field-1-11-6").attr("price", 500);
    $("#form-field-1-11-7").attr("price", 1250);
    $("#form-field-1-11-8").attr("price", 500);
    $("#form-field-1-11-9").attr("price", 700);
    options[0] = $("#form-field-1-11-1");
    options[1] = $("#form-field-1-11-2");
    options[2] = $("#form-field-1-11-3");
    options[3] = $("#form-field-1-11-4");
    options[4] = $("#form-field-1-11-5");
    options[5] = $("#form-field-1-11-6");
    options[6] = $("#form-field-1-11-7");
    options[7] = $("#form-field-1-11-8");
    options[8] = $("#form-field-1-11-9");
    var total = 0;
    $.each(options, function() {
        this.on("change", function() {
            total += this.attr("price");
        });
    });
    $("#sum-field").val(total);
}

thanks!!! 谢谢!!!

Your code is a lot more complex than it needs to be. 你的代码比它需要复杂得多

Firstly, you should use data-* attributes to assign custom data to an element. 首先,您应该使用data-*属性将自定义数据分配给元素。 Creating your own non-standard attributes will mean your HTML is invalid and can lead to other issues. 创建自己的非标准属性意味着您的HTML无效并可能导致其他问题。 Also, if your code is relying on the price attribute, then it should be in the DOM when the page loads. 此外,如果您的代码依赖于price属性,那么当页面加载时它应该在DOM中。

Secondly there's no need to build an array of single elements. 其次,不需要构建单个元素的数组。 You can select them all in to a single jQuery object and set a change() event handler on them in a single call. 您可以将它们全部选择到单个jQuery对象中,并在一次调用中为它们设置change()事件处理程序。 I grouped them by class in the below example. 我在下面的例子中按class对它们进行了分组。

Lastly you can get the total of all the prices by looping through the :checked boxes and adding up their prices. 最后,您可以通过以下方式获取所有价格的总和:checked复选框并将其价格相加。 Try this: 试试这个:

 $('.checkbox').change(function() { var total = 0; $('.checkbox:checked').each(function() { total += parseFloat($(this).data('price')); }); $("#sum-field").val(total); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" id="form-field-1-11-1" class="checkbox" data-price="500" /> <input type="checkbox" id="form-field-1-11-2" class="checkbox" data-price="500" /> <input type="checkbox" id="form-field-1-11-3" class="checkbox" data-price="0" /> <input type="checkbox" id="form-field-1-11-4" class="checkbox" data-price="300" /> <!-- other checkboxes here... --> <input type="text" id="sum-field" /> 

To get the value of the Value attribute you can do something like this: 要获取Value属性的值,您可以执行以下操作:

$("input[type='checkbox']").val();

Or if you have set a class or id for it, you can: 或者,如果您为其设置了类或ID,则可以:

$('#check_id').val();
$('.check_class').val();

However this will return the same value whether it is checked or not, this can be confusing as it is differnt to the submitted form behaviour. 但是,无论是否检查,这都将返回相同的值,这可能会造成混淆,因为它与提交的表单行为不同。

To check whether it is checked or not, do: 要检查是否已选中,请执行以下操作:

if ($('#check_id').is(":checked"))
{
  // it is checked
}

You just forgot to parse the return value to a number: 您只是忘了将返回值解析为数字:

parseInt(this.attr("price"));

the attr() function is returning a string value. attr()函数返回一个字符串值。

暂无
暂无

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

相关问题 如何在 javascript/jquery 中获得选中的复选框标题值? - How can i get a checked Checkbox title value in javascript/jquery? 当复选框被选中或复选框未选中删除值时,如何在另一个文本框中获得复选框值和文本框值的总和 - how can i get sum of checkbox value and textbox value in another textbox when checkbox is checked or if checkbox unchecked remove the value 反应:获取自定义复选框的选中属性 - react: get custom checkbox's checked attribute 如何通过在 angular 8 中选中的复选框获取表的 td 值 - How to get table's td value by checkbox checked in angular 8 如何获取已选中(复选框)的值并在本机反应中自动完成 textInput - How can I get the value of checked (checkbox) and autocomplete a textInput in react native 如何获得在“更新”模式弹出窗口中选中复选框的值? - How can I get the value that comes true with the checkbox checked in the "update" modal popup? 如何获得复选框的最新检查值? - How do i get the latest checked value of the checkbox? 如何获取多个复选框的值并使用复选框中的值执行增量 - how can i get value of multiple checked box and perform increment using value from checked box PHP和JS-如何选中并输入复选框以获取价值? - PHP and JS - How to get value from chosen checkbox is checked and enter? 如何从使用 javascript 检查的复选框中获取值? - How to get value from checkbox checked using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM