简体   繁体   English

document.querySelectorAll选择特定输入的ID

[英]document.querySelectorAll select id of specific input

I have a table which has two separate checkbox inputs. 我有一个表,其中有两个单独的复选框输入。 When selecting the first input there is a cumulative amount which calculates. 选择第一个输入时,会计算出一个累计量。 If you select the second checkbox, the function errors (because of the duplicate input). 如果选择第二个复选框,则功能会出错(由于输入重复)。

Function 功能

function updateTotals() {
    var sum = Array.prototype.reduce.call(document.querySelectorAll("input.check:checked"),(a,v) => a + parseFloat(v.dataset.totalAmount), 0);
    $('#checkedTotal').val(sum);
};

What I need to do is separate the inputs, maybe using a class. 我需要做的是分开输入,也许使用一个类。 I cant seem to get the syntax, or may be barking up the wrong tree. 我似乎无法获取语法,或者可能是吠错了树。 Something like... 就像是...

function updateTotals() {
    var sum = Array.prototype.reduce.call(document.querySelectorAll ('.input').check:checked,(a,v) => a + parseFloat(v.dataset.totalAmount), 0);
    $('#checkedTotal').val(sum);
};

I would add this as an jsfiddle example, but the table is in a Salesforce Visualforce Page, using apex fields. 我将其添加为jsfiddle示例,但该表位于Salesforce Visualforce页面中,使用顶点字段。

Thanks in advance 提前致谢

UPDATE UPDATE

Added HTML 添加了HTML

<div style="width:50%;">
<form id="j_id0:j_id2" name="j_id0:j_id2" method="post">
<input type="hidden" name="j_id0:j_id2" value="j_id0:j_id2">


    <input disabled="disabled" id="checkedTotal" name="amount" placeholder="Selected Amount" step=".02" type="number">

    <table id="invoicesTable" style="width:100%;">
        <thead class="tableHeadBlue">
            <tr>
                <td>Select</td>
                <td>Date</td>
                <td>Type</td>
                <td>Order</td>
                <td>Amount</td>
                <td>id</td>
                <td>Select2</td>
            </tr>
        </thead>
        <tbody>
                <tr>
                    <td>
                        <label class="formCheck"><input id="j_id0:j_id2:j_id4:0:inputId" type="checkbox" name="j_id0:j_id2:j_id4:0:inputId" class="check" onchange="updateTotals();" style="font-size:26px;" data-total-amount="458.00">
                        </label>
                    </td>
                    <td><span id="j_id0:j_id2:j_id4:0:j_id7">19/04/2018</span>
                        <span style="color:red;">
                        </span>
                    </td>
                    <td>Invoice
                    </td>
                    <td>
                        <span style="color:Black"><span id="j_id0:j_id2:j_id4:0:j_id14">00006648</span>
                        </span>
                    </td>
                    <td><span id="j_id0:j_id2:j_id4:0:j_id16">$458.00</span></td>
                    <td><span id="j_id0:j_id2:j_id4:0:j_id18">8015D000000CsiH</span></td>
                    <td><input type="checkbox" name="j_id0:j_id2:j_id4:0:j_id20" class="check" style="font-size:26px;"></td>
                </tr>
                <tr>
                    <td>
                        <label class="formCheck"><input id="j_id0:j_id2:j_id4:1:inputId" type="checkbox" name="j_id0:j_id2:j_id4:1:inputId" class="check" onchange="updateTotals();" style="font-size:26px;" data-total-amount="200.00">
                        </label>
                    </td>
                    <td><span id="j_id0:j_id2:j_id4:1:j_id7">21/06/2018</span>
                        <span style="color:red;">
                        </span>
                    </td>
                    <td>Invoice
                    </td>
                    <td>
                        <span style="color:Black"><span id="j_id0:j_id2:j_id4:1:j_id14">00006849</span>
                        </span>
                    </td>
                    <td><span id="j_id0:j_id2:j_id4:1:j_id16">$200.00</span></td>
                    <td><span id="j_id0:j_id2:j_id4:1:j_id18">8015D000000DEuB</span></td>
                    <td><input type="checkbox" name="j_id0:j_id2:j_id4:1:j_id20" class="check" style="font-size:26px;"></td>
                </tr>
                <tr>
                    <td>
                        <label class="formCheck"><input id="j_id0:j_id2:j_id4:2:inputId" type="checkbox" name="j_id0:j_id2:j_id4:2:inputId" class="check" onchange="updateTotals();" style="font-size:26px;" data-total-amount="500.00">
                        </label>
                    </td>
                    <td>
                        <span style="color:red;"><span id="j_id0:j_id2:j_id4:2:j_id9">22/06/2018</span>
                        </span>
                    </td>
                    <td><span style="color:red;">Credit</span>
                    </td>
                    <td>
                        <span style="color:red"><span id="j_id0:j_id2:j_id4:2:j_id14">00006852</span>
                        </span>
                    </td>
                    <td><span id="j_id0:j_id2:j_id4:2:j_id16">$500.00</span></td>
                    <td><span id="j_id0:j_id2:j_id4:2:j_id18">8015D000000DHKW</span></td>
                    <td><input type="checkbox" name="j_id0:j_id2:j_id4:2:j_id20" class="check" style="font-size:26px;"></td>
                </tr>
        </tbody>
    </table><div id="j_id0:j_id2:j_id24"></div>
</form>
</div>

Add class first to first checkbox in <tr> and add class second to second checkbox in <tr> . first将类添加到<tr>第一个复选框,然后将类second个添加到<tr>第二个复选框。

updateTotals only query checkbox which has class first to avoid second . updateTotals仅查询具有first类的复选框,以避免second类。

 function updateTotals() { var sum = Array.prototype.reduce.call(document.querySelectorAll("input.check.first:checked"),(a,v) => a + parseFloat(v.dataset.totalAmount), 0); $('#checkedTotal').val(sum); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div style="width:50%;"> <form id="j_id0:j_id2" name="j_id0:j_id2" method="post"> <input type="hidden" name="j_id0:j_id2" value="j_id0:j_id2"> <input disabled="disabled" id="checkedTotal" name="amount" placeholder="Selected Amount" step=".02" type="number"> <table id="invoicesTable" style="width:100%;"> <thead class="tableHeadBlue"> <tr> <td>Select</td> <td>Date</td> <td>Type</td> <td>Order</td> <td>Amount</td> <td>id</td> <td>Select2</td> </tr> </thead> <tbody> <tr> <td> <label class="formCheck"><input id="j_id0:j_id2:j_id4:0:inputId" type="checkbox" name="j_id0:j_id2:j_id4:0:inputId" class="check first" onchange="updateTotals();" style="font-size:26px;" data-total-amount="458.00"> </label> </td> <td><span id="j_id0:j_id2:j_id4:0:j_id7">19/04/2018</span> <span style="color:red;"> </span> </td> <td>Invoice </td> <td> <span style="color:Black"><span id="j_id0:j_id2:j_id4:0:j_id14">00006648</span> </span> </td> <td><span id="j_id0:j_id2:j_id4:0:j_id16">$458.00</span></td> <td><span id="j_id0:j_id2:j_id4:0:j_id18">8015D000000CsiH</span></td> <td><input type="checkbox" name="j_id0:j_id2:j_id4:0:j_id20" class="check second" style="font-size:26px;"></td> </tr> <tr> <td> <label class="formCheck"><input id="j_id0:j_id2:j_id4:1:inputId" type="checkbox" name="j_id0:j_id2:j_id4:1:inputId" class="check first" onchange="updateTotals();" style="font-size:26px;" data-total-amount="200.00"> </label> </td> <td><span id="j_id0:j_id2:j_id4:1:j_id7">21/06/2018</span> <span style="color:red;"> </span> </td> <td>Invoice </td> <td> <span style="color:Black"><span id="j_id0:j_id2:j_id4:1:j_id14">00006849</span> </span> </td> <td><span id="j_id0:j_id2:j_id4:1:j_id16">$200.00</span></td> <td><span id="j_id0:j_id2:j_id4:1:j_id18">8015D000000DEuB</span></td> <td><input type="checkbox" name="j_id0:j_id2:j_id4:1:j_id20" class="check second" style="font-size:26px;"></td> </tr> <tr> <td> <label class="formCheck"><input id="j_id0:j_id2:j_id4:2:inputId" type="checkbox" name="j_id0:j_id2:j_id4:2:inputId" class="check first" onchange="updateTotals();" style="font-size:26px;" data-total-amount="500.00"> </label> </td> <td> <span style="color:red;"><span id="j_id0:j_id2:j_id4:2:j_id9">22/06/2018</span> </span> </td> <td><span style="color:red;">Credit</span> </td> <td> <span style="color:red"><span id="j_id0:j_id2:j_id4:2:j_id14">00006852</span> </span> </td> <td><span id="j_id0:j_id2:j_id4:2:j_id16">$500.00</span></td> <td><span id="j_id0:j_id2:j_id4:2:j_id18">8015D000000DHKW</span></td> <td><input type="checkbox" name="j_id0:j_id2:j_id4:2:j_id20" class="check second" style="font-size:26px;"></td> </tr> </tbody> </table><div id="j_id0:j_id2:j_id24"></div> </form> </div> 

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

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