jQuery实现全选和反选和临时页面删除

来源:互联网 发布:mac双系统怎么切换系统 编辑:程序博客网 时间:2024/06/14 06:54
本人用的是3层调的数据库动态生成的table表格;代码如下:
public void load()
        {
            
            StringBuilder sb = new StringBuilder();
            sb.Append("<table>");
            sb.Append("<tr><td>选择</td><td>编号</td><td>标题</td><td>内容</td><td>图片</td></tr>");
            SqlDataReader read = WebApplication1.DataCass.DbHelperSQL.ExecuteReader("select id,title,content,imagepath FROM temporary");
            while (read.Read())
            {
                string id =Convert.ToString(read.GetSqlInt32(read.GetOrdinal("id")));
                string title = read.GetString(read.GetOrdinal("title"));
                string content = read.GetString(read.GetOrdinal("content"));
                string img = read.GetString(read.GetOrdinal("imagepath"));
                sb.Append("<tr class=tr><td><input type=checkbox name=check /></td><td>" + id + "</td><td>" + title + "</td><td>" + content + "</td><td><img class=gg src=" + img + "/></td></tr>");
            }
          
            sb.Append("</table>");
            insert.InnerHtml = sb.ToString();
        }
接下来就是jquery的代码了:
 <script type="text/javascript">
        $(function () {
            //全选
            $('#checkall').click(function () {
                $('[name=check]').attr('checked', $(this).is(':checked'))
                //                if ($('[name=check]').checked=false) {
                //                    $('#checkall').attr('checked',false)
                //                }
            })
            //反选
            $('#checkother').click(function () {
                $('[name=check]').each(function () {
                    if ($(this).is(':checked')) {
                        $(this).attr('checked', false);
                    }
                    else { $(this).attr('checked', true); }
                })
            })
            //删除代码
            $('#btn').click(function () {
                $('[name=check]:checked').each(function () {
                    if ($(this).is(':checked')) {
                        //方法一
                        //var i = $('table tr td input').index(this)
                        //$('table tr td :checkbox:checked').parent().parent().remove()

                        //方法二
                        var i = $('table tr td input').index(this)+1
                        $('table').find('tr').filter(':eq('+i+')').remove()
                    }
                })

                //$('tr:eq(' +  + ')').remove();
            })
            $('table img').mouseover(function (e) {
                $('#imgg').attr('src', $(this).attr('src'))
                $('#imgg').css('left', 735);
                $('#imgg').css('top', e.pageY - 10);
                $('#imgg').show()
            })
            $('table img').mouseout(function () {
                $(this).attr('style', 'background-color:rgb(239,254,253)');
                $('#imgg').hide()
            })
        })
    </script>