JS實戰 · 複選框全選操作

來源:互聯網
上載者:User

標籤:數組   font   cti   check   har   checkbox   狀態   http   屬性   

思路:1、擷取被選中checkbox,通過checked屬性的狀態完成;2、擷取被選中的checkbox的value值;3、求所有value的和sum;4、定義span地區儲存和sum; 代碼如下:<html><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>表單組件:全選操作</title>    <script type="text/javascript">        function checkAll(index){            var node = document.getElementsByName("all")[index];            var items = document.getElementsByName("item");            for(var i=0; i<items.length; i++){                items[i].checked = node.checked;            }        }    </script></head><body>    <p></p>    <input type="checkbox" name="all" onclick="checkAll(0)"/>全選<br/>    <input type="checkbox" name="item" value="100"/>商品1<br/>    <input type="checkbox" name="item" value="100"/>商品2<br/>    <input type="checkbox" name="item" value="100"/>商品3<br/>    <input type="checkbox" name="item" value="100"/>商品4<br/>    <input type="checkbox" name="item" value="100"/>商品5<br/>    <input type="checkbox" name="item" value="100"/>商品6<br/>    <input type="checkbox" name="all" onclick="checkAll(1)"/>全選</body></html> 代碼如下:<html><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>購物車顯示選中購買物品的總金額</title>    <script type="text/javascript">        function checkAll(index){            var node = document.getElementsByName("all")[index];            var items = document.getElementsByName("item");            for(var i=0; i<items.length; i++){                items[i].checked = node.checked;            }        }         function getSum(){            var items = document.getElementsByName("item");            var sum = 0;            for(var i=0; i<items.length; i++){                if(items[i].checked){                    sum += parseInt(items[i].value);                }            }            /*擷取單個節點用getElementById              擷取節點數組用getElementsById             */            var spanNode = document.getElementById("sum");            var str = sum + "元";            spanNode.innerHTML = str.fontsize(8);        }    </script></head><body>    <br/>    <input type="checkbox" name="all" onclick="checkAll(0)"/>全選<br/>    <input type="checkbox" name="item" value="105"/>商品1<br/>    <input type="checkbox" name="item" value="214"/>商品2<br/>    <input type="checkbox" name="item" value="113"/>商品3<br/>    <input type="checkbox" name="item" value="77"/>商品4<br/>    <input type="checkbox" name="item" value="91"/>商品5<br/>    <input type="checkbox" name="item" value="536"/>商品6<br/>    <input type="checkbox" name="all" onclick="checkAll(1)"/>全選<br/>    <input type="button" value="顯示總金額" onclick="getSum()"/><span id="sum"></span></body></html> 

JS實戰 · 複選框全選操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.