關於jquery全選反選 大量刪除的一點心得

來源:互聯網
上載者:User

標籤:ems   擷取   doc   conf   success   remove   nal   window   需要   

廢話不多說直接上代碼:

下面是jsp頁面的html代碼:

<table id="contentTable" class="">        <thead>            <tr>             <th><input type="checkbox" id="All" value="全選/全不選" />全選/全不選</th>             </tr>        </thead>        <tbody>        <c:forEach items="${page.list}" var="advAdvertisement">            <tr>                <td><input type="checkbox" value="${advAdvertisement.id}" name="checkbox" class="checkbox"/></td>            </tr>        </c:forEach>        </tbody>    </table>

然後是jquery:

         $(document).ready(function(){                        $(‘#All‘).change(function(){                            if($(‘#All‘).attr(‘checked‘)){                            $(‘.checkbox‘).attr(‘checked‘,true);                            }else{                $(‘.checkbox‘).removeAttr(‘checked‘);            }                            });        });

 

代碼簡單明了,相信聰明的你一看就懂。

最後是大量刪除選中的複選框資料:

<input id="btnSubmit" class="" onclick="deleteAll();" type="button" value="大量刪除"/><script type="text/javascript">                function deleteAll(){            if(confirm(‘確認要刪除選中的廣告嗎?‘)==true){                            var obj=document.getElementsByName(‘checkbox‘); //選擇所有name="‘test‘"的對象,返回數組             //取到對象數組後,我們來迴圈檢測它是不是被選中             var s=new Array();             for(var i=0; i<obj.length; i++){             if(obj[i].checked){                s[i]=obj[i].value;            } //如果選中,將value添加到數組s中             }             //那麼現在來檢測s的值就知道選中的複選框的值了             alert(s==‘‘?‘你還沒有選擇任何內容!‘:s);            alert(s);            $.ajax({                  type:‘POST‘,                  url:‘${ctx}/adv/advAdvertisement/deleteAll‘,                  data:{‘ids‘:s},                  traditional:true,                  dataType:‘text‘,                  success:function(data){alert(‘刪除成功!‘)}            });            window.location.href="";            }        }    </script>

大量刪除的時候需要去擷取每一個被選中的複選框的id,我們用一個數組把他們存起來,最重要的一點,是要在$.ajax中配置traditional:true這個屬性,如果不配置後台controller是不認可的。

 

後台接收的話直接用一個String[] ids數組接收就可以得到關於id的一個數組了,然後for迴圈盡情的做你想做的一切吧!

 

關於jquery全選反選 大量刪除的一點心得

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.