简体   繁体   English

使用Java的高级复选框

[英]Advanced Checkboxes with Javascript

I have a form that is a table built with php to pull a bunch of information from the database. 我有一个表单,该表单是用php构建的表,用于从数据库中提取一堆信息。 In these, i have a checkbox that, when clicked, pulls the value of the estimatedCost and throws it into a JavaScript function that calculates it and keeps a running total of all the objects checked. 在这些代码中,我有一个复选框,当单击该复选框时,它会提取EstimateCost的值并将其扔到JavaScript函数中,该函数对其进行计算并保持所有被检查对象的运行总数。

What i'm trying to do is create a check all and uncheck all option that will still pass the needed variables into the other javascript functions. 我想做的是创建一个全选并取消全选选项,该选项仍会将所需的变量传递到其他javascript函数中。 Let me demonstrate with some code: 让我用一些代码演示:

This draws the checkbox next to the title. 这将在标题旁边绘制复选框。

foreach($replace as $key => $value)
  {
    $jScript = 'onclick=\'calcTotals("'.$replace[$key]['estimatedCost'].'","'.$replace[$key]['optionItem_id_replaceOrRepair'].'","'.$replace[$key]['service_title'].'","'.$replace[$key]['maintenanceitem_id'].'");\'';
    $checkbox = sprintf('<input type="checkBox" name="ids[]" id="%s" value="%s" %s>', $replace[$key]['maintenanceitem_id'], $replace[$key]['maintenanceitem_id'], $jScript).'&nbsp;';
    $replace[$key]['title'] = $checkbox.$replace[$key]['title'];
    $replace[$key]['estimatedCost'] =  $replace[$key]['estimatedCost'];
  } 

This is the current check all and uncheck all links: 这是当前的全部选中并取消选中所有链接:

echo '<a href="#" onClick=\'setCheckboxes("budgeting", true); return false;\'>Check All</a>&nbsp;|&nbsp;';
  echo '<a href="#" onClick=\'setCheckboxes("budgeting", false); return false;\'>Uncheck All</a>';

Now the current functions i have in javascript: 现在我在javascript中具有当前功能:

function setBudgetCheckboxes(the_form, do_check) {
        var elts      = (typeof(document.forms[the_form].elements['ids[]']) != 'undefined')
                      ? document.forms[the_form].elements['ids[]']
                      : (typeof(document.forms[the_form].elements['ids[]']) != 'undefined')
              ? document.forms[the_form].elements['ids[]']
              : document.forms[the_form].elements['ids[]'];
        var elts_cnt  = (typeof(elts.length) != 'undefined')
                      ? elts.length
                      : 0;
        if (elts_cnt) {
            for (var i = 0; i < elts_cnt; i++) {
                elts[i].checked = do_check;
                var name = document.getElementById(name);
            } // end for
        } else {
            elts.checked = do_check;
        } // end if... else
        return true;
    }

And the other, which handles the clicks one at a time: 另一个,一次处理一次点击:

function calcTotals(amount, type, service, name) {
        if(amount[0] == '$') {
            amount = amount.substr(1,amount.length);
        }
        var id = type+"_"+service+"_selected";
        var grand_id = "Grand_selected";
        var grand_service_id = "Grand_"+service+"_selected";
        var type_id = type+"_selected";

        var checked = document.getElementById(name).checked;

        var multiplier = -1;

        if(checked) {
            multiplier = 1;
        }

        amount = amount * multiplier;

        addBudgetValue(amount, id);
        addBudgetValue(amount, grand_id);
        addBudgetValue(amount, grand_id+"_h");
        addBudgetValue(amount, type_id);
        addBudgetValue(amount, grand_service_id);
        addBudgetValue(amount, grand_service_id+"_h");
    }
function addBudgetValue(amount, id) {
        var current_value = document.getElementById(id).innerHTML;
        var curtmp = 0;
        if(current_value == "$0") {
            current_value = amount;
        }
        else {
            curtmp = parseFloat(current_value.substr(1,current_value.length));
            current_value = (curtmp+parseFloat(amount));
        }   
        var newVal = "$"+Number(current_value).toFixed(2);
        if(newVal == "$0.00")
            newVal = "$0";  
        document.getElementById(id).innerHTML = newVal;
    }

So the question is this: How do you get the check all to check all the boxes, and pass the information into the calcTotals function so that the values are added correctly? 所以问题是这样的:如何获取全部选中所有复选框的复选框,然后将信息传递到calcTotals函数中,以便正确添加值?

Here's a jsfiddle that does what (I think) you're looking for: http://jsfiddle.net/kz9gU/1/ 这是一个jsfiddle,它可以满足您的需求(我认为): http : //jsfiddle.net/kz9gU/1/

I'm using a custom data-* attribute to store the estimated cost (spec compliant as per HTML5), so it's defined in the checkboxes' tags. 我使用自定义data-*属性存储估算费用(符合HTML5规范),因此它在复选框的标签中定义。 Each checkbox then calls an updateTotal() function when checked/unchecked, but doesn't pass any arguments. 然后,每个复选框在选中/取消选中时都会调用updateTotal()函数,但不传递任何参数。 updateTotal() then loops through the checkboxes, reading each amount from the data-cost attribute, and displays the sum. 然后, updateTotal()遍历复选框,从data-cost属性读取每个金额,并显示总和。

The check/uncheck functions then don't need to calculate any values; 然后,检查/取消检查功能无需计算任何值; they just check/uncheck everything and call updateTotal() afterwards. 他们只是检查/取消选中所有内容,然后再调用updateTotal()

Edit: Heh, just realized you're not using jQuery. 编辑:嘿,刚刚意识到您没有使用jQuery。 I'm just starting to take jQuery for granted, even though I actually don't use jQuery myself. 即使我自己实际上并不使用jQuery,我也开始将jQuery视为理所当然。 What you see in the fiddle is probably the longest piece of jQuery-based JS, I've actually written :) 您在小提琴中看到的可能是最长的基于jQuery的JS,我实际上已经写过:)
At any rate, it's almost always a good idea to use some sort of library to smooth out browser differences, and jQuery is king of the heap right now. 无论如何,使用某种库来消除浏览器差异几乎总是一个好主意,而jQuery现在是堆中的王者。

But library or not, the code should give you an idea of how you could proceed. 但是无论是否存储库,代码都应该使您了解如何进行。

I hope I understood you currectly, you want a check/uncheck all feature? 希望我能对您有所了解,是否要选中/取消选中所有功能? http://jetlogs.org/jquery/jquery_select_all.html after that get the checkbox array with php and pass the information, in PHP do a loop to check the values of the array and execute your statements. http://jetlogs.org/jquery/jquery_select_all.html之后,用php获取复选框数组并传递信息,在PHP中执行一个循环以检查数组的值并执行您的语句。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM