搜索
首页php教程php手册php实战第九天

php实战第九天

Jun 13, 2016 am 10:57 AM
php事件取消可以实战绑定调用进行重复

1.jquery事件可以绑定N个,如果不进行取消就会重复调用绑定的事件。深感体会,搞了两小时终于发现其中奥妙。
以下代码不能单独运行的。需要 bootstrap和jquery
[javascript]  /**
* 用于显示对话框消息框
* 参数 title 消息标题
* 参数 content 消息内容
* 参数 buttomTitle 处理消息的按钮自定义的,比如确认删除
* 参数 fun 自定义按钮click事件
* 参数 passOnData 传递到自定义fun里的参数
*/ 
function show_Msg (title,content,buttomTitle,fun,passOnData) { 
    $("#msg #myModalLabel").html(title); 
    $("#msg .modal-body").html(content); 
    $('#msg #msg_c').html(buttomTitle).click(function(){ 
        fun(passOnData);//调用自定义的函数,以及传递自定义的数据  
        $('#msg').modal('hide');//点击完就把窗口隐藏了  
        $(this).unbind('click');//如果不取消事件,那么将重复调用。。  
    });; 
    $('#msg').modal('show'); 

/**
* 用于显示对话框消息框
* 参数 title 消息标题
* 参数 content 消息内容
* 参数 buttomTitle 处理消息的按钮自定义的,比如确认删除
* 参数 fun 自定义按钮click事件
* 参数 passOnData 传递到自定义fun里的参数
*/
function show_Msg (title,content,buttomTitle,fun,passOnData) {
 $("#msg #myModalLabel").html(title);
 $("#msg .modal-body").html(content);
 $('#msg #msg_c').html(buttomTitle).click(function(){
  fun(passOnData);//调用自定义的函数,以及传递自定义的数据
  $('#msg').modal('hide');//点击完就把窗口隐藏了
  $(this).unbind('click');//如果不取消事件,那么将重复调用。。
 });;
 $('#msg').modal('show');
}html消息框模板
[html]   

 


调用例程
[javascript]  show_Msg('标题要长长长长的','这里可以写html比如加粗的的字体噢','删除',function(e){ 
    alert(e); 
},'这里是点击删除后我传递过去的数据'); 

show_Msg('标题要长长长长的','这里可以写html比如加粗的的字体噢','删除',function(e){
 alert(e);
},'这里是点击删除后我传递过去的数据');

[javascript]  function admin_content_del (id) { 
    var data=listData[id]; 
    show_Msg('确认删除',data.content,'确认删除',function(delId){ 
        $.ajax({ 
          url: 'http://localhost/l/index.php', 
          type: 'get', 
          dataType: 'json', 
          data: { 
            m: 'admin', 
            a: 'delcontent', 
            id: delId 
          }, 
          complete: function(xhr, textStatus) { 
            //called when complete  
          }, 
          success: function(data, textStatus, xhr) { 
            if(data.state='ok'){ 
                admin_content(1); 
                show_Msg_success('删除成功'); 
                 
            }else{ 
                show_Msg_success('删除失败'); 
            } 
          }, 
          error: function(xhr, textStatus, errorThrown) { 
            //called when there is an error  
          } 
        }); 
         
    },data.id); 

function admin_content_del (id) {
 var data=listData[id];
 show_Msg('确认删除',data.content,'确认删除',function(delId){
  $.ajax({
    url: 'http://localhost/l/index.php',
    type: 'get',
    dataType: 'json',
    data: {
     m: 'admin',
     a: 'delcontent',
     id: delId
    },
    complete: function(xhr, textStatus) {
      //called when complete
    },
    success: function(data, textStatus, xhr) {
     if(data.state='ok'){
      admin_content(1);
      show_Msg_success('删除成功');
      
     }else{
      show_Msg_success('删除失败');
     }
    },
    error: function(xhr, textStatus, errorThrown) {
      //called when there is an error
    }
  });
  
 },data.id);
}


2.setTimeout延迟执行事件,这消息过真不错,挺常用的,但是这清空消息的方式,简单粗暴。


[javascript]  /**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/ 
function show_Msg_success(content,face){ 
    if (face==null) { 
        face=true; 
    } 
 
    face = face?'success':'error'; 
     
    strTag='

'+content+'
'; 
 
    $(strTag).prependTo('#main'); 
 
    setTimeout(function(){ 
        $(".alert").alert('close'); 
    },3000); 

/**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/
function show_Msg_success(content,face){
 if (face==null) {
  face=true;
 }

 face = face?'success':'error';
 
    strTag='

'+content+'
';

 $(strTag).prependTo('#main');

 setTimeout(function(){
  $(".alert").alert('close');
 },3000);
}


3.checkbox选中还是纯dom操作好. jquery的arrt()方法坑爹
checkbox.attr('checked',$(this).get()[0].checked);

选中复选框,但是第一次有效,第二次也有效,第三次以后,离奇失效了。问题不名真相。
[javascript]  function click_tr() { 
    var checkbox = $("#mainData tr input"); 
 
    checkbox.eq(0).click(function() { 
        //checkbox.attr('checked',$(this).get()[0].checked);  
 
        bool = $(this).get()[0].checked; 
 
        for (var i = 1; i             checkbox[i].checked = bool; 
        }; 
 
    }); 
 
    $("#mainData tr").each(function(index) { 
        $(this).click(function() { 
 
            if (index == 0) { 
 
            } else { 
                var val = checkbox.eq(index); 
                    val = val.get()[0]; 
                var bool = val.checked; 
 
                //alert(index);  
                if (bool) { 
                    val.checked = false; 
                } else { 
                    val.checked = true; 
                } 
            } 
             
        }); 
 
    }); 

function click_tr() {
 var checkbox = $("#mainData tr input");

 checkbox.eq(0).click(function() {
  //checkbox.attr('checked',$(this).get()[0].checked);

  bool = $(this).get()[0].checked;

  for (var i = 1; i    checkbox[i].checked = bool;
  };

 });

 $("#mainData tr").each(function(index) {
  $(this).click(function() {

   if (index == 0) {

   } else {
    var val = checkbox.eq(index);
        val = val.get()[0];
    var bool = val.checked;

    //alert(index);
    if (bool) {
     val.checked = false;
    } else {
     val.checked = true;
    }
   }
   
  });

 });进行探究一番写了一测试代码,但是依然无果,不明真相的全选不了。
[html]   

 
     
     
 
 
 
 
 
     
     
     
     
     
     
 
全选 
取消选中 
 
 
 
 
 
 



 
 




 
 
 
 
 
 

全选
取消选中


 $("#quxiao").click(function(){
  $("#myform input").attr('checked',false);
 });
});


4.说了这么多都没上效果图呢.

 

5.标题说是php实战,怎么前面帖的都是php代码。。下面帖出处理批量删除的php代码
[php] /**
* 用于批量删除留言
*/ 
function batchDelContent(){ 
    $json['state']="no"; 
 
    $data=Array(); 
 
    $arr=$_POST['delcontent']; 
    if (count($arr)>0) { 
        $json['state']="ok"; 
    } 
 
    foreach ($arr as $value) { 
 
        $result=$this->db 
             ->where("id=".$value) 
             ->table("data") 
             ->delete(); 
 
        if ($result) { 
            $arr_data['state']="ok"; 
        }else{ 
            $arr_data['state']="no"; 
        } 
        $arr_data['id']=$value; 
 
        $data[]=$arr_data; 
    } 
 
    $json['data']=$data; 
    echo json_encode($json); 

  /**
  * 用于批量删除留言
  */
  function batchDelContent(){
   $json['state']="no";

   $data=Array();

   $arr=$_POST['delcontent'];
   if (count($arr)>0) {
    $json['state']="ok";
   }

   foreach ($arr as $value) {

    $result=$this->db
      ->where("id=".$value)
      ->table("data")
      ->delete();

    if ($result) {
     $arr_data['state']="ok";
    }else{
     $arr_data['state']="no";
    }
    $arr_data['id']=$value;

    $data[]=$arr_data;
   }

   $json['data']=$data;
   echo json_encode($json);
  }今天写的php代码也就这么点了。。大部分都是javascript


我把admin.js帖出来给大家观赏一下。
[javascript]  // JavaScript Document  
$(document).ready(function(e) { 
 
    $("#menu a").click(function() { 
 
        switch ($(this).text()) { 
            case '所有留言': 
                admin_content(1); 
                break; 
 
            case '基本设置': 
                $("#main #mainData").load('admin_config.html?r='+Math.random()); 
                break; 
 
            default: 
                break; 
        } 
 
 
 
    }); 
 
$("#main #mainData").load('admin_config.html?r='+Math.random()); 
    //$("#start").click();  
}); 
 
 
/**
*留言管理
*/ 
listData=null; 
function admin_content(page) { 
 
    $.ajax({ 
        url: 'http://localhost/l/admin.php?m=admin&a=content&page=' + page + '&rand=' + Math.random(), 
        type: 'get', 
        dataType: 'json', 
        data: {}, 
        complete: function(xhr, textStatus) { 
            //called when complete  
        }, 
        success: function(json, textStatus, xhr) { 
            if (json['state'] == 'ok') { 
                var page_start = json['start'];//分页开始  
                var page_end = json['end'];//分页结束  
                var page_page = json['page'];//分页当前页面  
                    listData = json['data'];//分页数据  
 
                table_html=''; 
                table_html+='批量删除'; 
/**             
* 生成 表格内容
*/               
                table_html+= '

'; 
                for (i = 0; i                     var trClass = (i % 2 == 0) ? 'class="info"' : ''; 
 
                    //var tr_html = '';  
                    tr_html = ''; 
                    tr_html+=''; 
                    //删除  
 
                    tr_html+=''; 
                    tr_html+=''; 
                    tr_html+=''; 
 
                    table_html += tr_html; 
                } 
                table_html += '
操作 用户名 留言内容 发表时间
删除 ' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
'+listData[i].id+'' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
'; 
 
/**
*生成分页
*/ 
                var page_html = '
'; 
 
 
                var mainData = $("#main #mainData"); 
                mainData.html(table_html); 
                mainData.append(page_html); 
 
                admin_content_page(page_page,page_end); //挂接分页点击事件  
                click_tr();//挂接行点击事件;  
            } 
 
 
 
            //alert(json.data);  
        }, 
        error: function(xhr, textStatus, errorThrown) { 
            //called when there is an error  
        } 
    }); 

 
/**
* 挂机分页事件
* 参数 page_page 当前分页
* 参数 page_end  分页数量
*/ 
function admin_content_page(page_page,page_end) { 
    $("#mainPage a").click(function() { 
        var charStr = $(this).text(); 
        var num = charStr; 
        if (charStr == "»") { 
            num = parseInt(page_page) + 1; 
            if (page_end                 return; 
            } 
 
        } else if (charStr == "«") { 
            num = parseInt(page_page) - 1; 
            if (num                 return; 
            } 
        } 
 
        admin_content(num); 
 
    }); 

 
 
 
function getLocalTime(nS) { 
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' '); 

 
function admin_content_del (id) { 
    var data=listData[id]; 
    show_Msg('确认删除',data.content,'确认删除',function(delId){ 
        $.ajax({ 
          url: 'http://localhost/l/index.php', 
          type: 'get', 
          dataType: 'json', 
          data: { 
            m: 'admin', 
            a: 'delcontent', 
            id: delId 
          }, 
          complete: function(xhr, textStatus) { 
            //called when complete  
          }, 
          success: function(data, textStatus, xhr) { 
            if(data.state='ok'){ 
                admin_content(1); 
                show_Msg_success('删除成功'); 
                 
            }else{ 
                show_Msg_success('删除失败'); 
            } 
          }, 
          error: function(xhr, textStatus, errorThrown) { 
            //called when there is an error  
          } 
        }); 
         
    },data.id); 

 
function admin_content_del_pl() { 
 
    var checkbox = $("#mainData :checked"); 
 
    var listId = Array(); 
 
    checkbox.each(function() { 
        //alert($(this).get()[0].name);  
        if ($(this).get()[0].name == 'delcontent[]') { 
            listId.unshift($(this).val()); 
        } 
    }); 
 
    $.ajax({ 
      url: 'http://localhost/l/admin.php?m=admin&a=batchDelContent', 
      type: 'POST', 
      dataType: 'json', 
      data: {delcontent: listId 
      }, 
      complete: function(xhr, textStatus) { 
        //called when complete  
      }, 
      success: function(json, textStatus, xhr) { 
        if(json.state=='ok'){ 
            var data = json.data; 
            for (var i = 0; i                 if(data[i]['state']=='ok'){ 
                    show_Msg_success(data[i].id + '删除成功'); 
                }else{ 
                    show_Msg_success(data[i].id + '删除失败',false); 
                } 
            }; 
 
        }else{ 
 
        } 
        admin_content(1); 
      }, 
      error: function(xhr, textStatus, errorThrown) { 
        //called when there is an error  
      } 
    }); 
     
//  alert(listId);  

 
 
/**
* 用于显示对话框消息框
* 参数 title 消息标题
* 参数 content 消息内容
* 参数 buttomTitle 处理消息的按钮自定义的,比如确认删除
* 参数 fun 自定义按钮click事件
* 参数 passOnData 传递到自定义fun里的参数
*/ 
function show_Msg (title,content,buttomTitle,fun,passOnData) { 
    $("#msg #myModalLabel").html(title); 
    $("#msg .modal-body").html(content); 
    $('#msg #msg_c').html(buttomTitle).click(function(){ 
        fun(passOnData);//调用自定义的函数,以及传递自定义的数据  
        $('#msg').modal('hide');//点击完就把窗口隐藏了  
        $(this).unbind('click');//如果不取消事件,那么将重复调用。。  
    });; 
    $('#msg').modal('show'); 

/**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/ 
function show_Msg_success(content,face){ 
    if (face==null) { 
        face=true; 
    } 
 
    face = face?'success':'error'; 
     
    strTag='
'+content+'
'; 
 
    $(strTag).prependTo('#main'); 
 
    setTimeout(function(){ 
        $(".alert").alert('close'); 
    },3000); 

 
//show_Msg_content('啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊所得税')  
/*
show_Msg('标题要长长长长的','这里可以写html比如加粗的的字体噢','删除',function(e){
    alert(e);
},'这里是点击删除后我传递过去的数据');
*/ 
 
function click_tr() { 
    var checkbox = $("#mainData tr input"); 
 
    checkbox.eq(0).click(function() { 
        //checkbox.attr('checked',$(this).get()[0].checked);  
 
        bool = $(this).get()[0].checked; 
 
        for (var i = 1; i             checkbox[i].checked = bool; 
        }; 
 
    }); 
 
    $("#mainData tr").each(function(index) { 
        $(this).click(function() { 
 
            if (index == 0) { 
 
            } else { 
                var val = checkbox.eq(index); 
                    val = val.get()[0]; 
                var bool = val.checked; 
 
                //alert(index);  
                if (bool) { 
                    val.checked = false; 
                } else { 
                    val.checked = true; 
                } 
            } 
 
        }); 
 
    }); 

// JavaScript Document
$(document).ready(function(e) {

 $("#menu a").click(function() {

  switch ($(this).text()) {
   case '所有留言':
    admin_content(1);
    break;

   case '基本设置':
    $("#main #mainData").load('admin_config.html?r='+Math.random());
    break;

   default:
    break;
  }

 

 });

$("#main #mainData").load('admin_config.html?r='+Math.random());
 //$("#start").click();
});


/**
*留言管理
*/
listData=null;
function admin_content(page) {

 $.ajax({
  url: 'http://localhost/l/admin.php?m=admin&a=content&page=' + page + '&rand=' + Math.random(),
  type: 'get',
  dataType: 'json',
  data: {},
  complete: function(xhr, textStatus) {
   //called when complete
  },
  success: function(json, textStatus, xhr) {
   if (json['state'] == 'ok') {
    var page_start = json['start'];//分页开始
    var page_end = json['end'];//分页结束
    var page_page = json['page'];//分页当前页面
        listData = json['data'];//分页数据

    table_html='';
    table_html+='批量删除';
/**    
* 生成 表格内容
*/    
    table_html+= '

';
    for (i = 0; i      var trClass = (i % 2 == 0) ? 'class="info"' : '';

     //var tr_html = '

';
     tr_html = '';
     tr_html+='';
     //删除

     tr_html+='

';
     tr_html+='';
     tr_html+='';

     table_html += tr_html;
    }
    table_html += '

操作 用户名 留言内容 发表时间
删除 ' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
'+listData[i].id+' ' + listData[i].userName + '
' + listData[i].content + '
' + getLocalTime(listData[i].time) + '
';

/**
*生成分页
*/
    var page_html = '

';


    var mainData = $("#main #mainData");
    mainData.html(table_html);
    mainData.append(page_html);

    admin_content_page(page_page,page_end); //挂接分页点击事件
    click_tr();//挂接行点击事件;
   }

 

   //alert(json.data);
  },
  error: function(xhr, textStatus, errorThrown) {
   //called when there is an error
  }
 });
}

/**
* 挂机分页事件
* 参数 page_page 当前分页
* 参数 page_end  分页数量
*/
function admin_content_page(page_page,page_end) {
 $("#mainPage a").click(function() {
  var charStr = $(this).text();
  var num = charStr;
  if (charStr == "»") {
   num = parseInt(page_page) + 1;
   if (page_end     return;
   }

  } else if (charStr == "«") {
   num = parseInt(page_page) - 1;
   if (num     return;
   }
  }

  admin_content(num);

 });
}

 

function getLocalTime(nS) {
 return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
}

function admin_content_del (id) {
 var data=listData[id];
 show_Msg('确认删除',data.content,'确认删除',function(delId){
  $.ajax({
    url: 'http://localhost/l/index.php',
    type: 'get',
    dataType: 'json',
    data: {
     m: 'admin',
     a: 'delcontent',
     id: delId
    },
    complete: function(xhr, textStatus) {
      //called when complete
    },
    success: function(data, textStatus, xhr) {
     if(data.state='ok'){
      admin_content(1);
      show_Msg_success('删除成功');
      
     }else{
      show_Msg_success('删除失败');
     }
    },
    error: function(xhr, textStatus, errorThrown) {
      //called when there is an error
    }
  });
  
 },data.id);
}

function admin_content_del_pl() {

 var checkbox = $("#mainData :checked");

 var listId = Array();

 checkbox.each(function() {
  //alert($(this).get()[0].name);
  if ($(this).get()[0].name == 'delcontent[]') {
   listId.unshift($(this).val());
  }
 });

 $.ajax({
   url: 'http://localhost/l/admin.php?m=admin&a=batchDelContent',
   type: 'POST',
   dataType: 'json',
   data: {delcontent: listId
   },
   complete: function(xhr, textStatus) {
     //called when complete
   },
   success: function(json, textStatus, xhr) {
     if(json.state=='ok'){
      var data = json.data;
      for (var i = 0; i        if(data[i]['state']=='ok'){
        show_Msg_success(data[i].id + '删除成功');
       }else{
     show_Msg_success(data[i].id + '删除失败',false);
       }
      };

     }else{

     }
     admin_content(1);
   },
   error: function(xhr, textStatus, errorThrown) {
     //called when there is an error
   }
 });
 
// alert(listId);
}


/**
* 用于显示对话框消息框
* 参数 title 消息标题
* 参数 content 消息内容
* 参数 buttomTitle 处理消息的按钮自定义的,比如确认删除
* 参数 fun 自定义按钮click事件
* 参数 passOnData 传递到自定义fun里的参数
*/
function show_Msg (title,content,buttomTitle,fun,passOnData) {
 $("#msg #myModalLabel").html(title);
 $("#msg .modal-body").html(content);
 $('#msg #msg_c').html(buttomTitle).click(function(){
  fun(passOnData);//调用自定义的函数,以及传递自定义的数据
  $('#msg').modal('hide');//点击完就把窗口隐藏了
  $(this).unbind('click');//如果不取消事件,那么将重复调用。。
 });;
 $('#msg').modal('show');
}
/**
* 用于显示顶部消息。显示的消息3秒后自动销毁。
* 参数 content 消息内容
* 参数 face 消息的样式,真,为成功绿色的;假,为错误红色的
*/
function show_Msg_success(content,face){
 if (face==null) {
  face=true;
 }

 face = face?'success':'error';
 
    strTag='

'+content+'
';

 $(strTag).prependTo('#main');

 setTimeout(function(){
  $(".alert").alert('close');
 },3000);
}

//show_Msg_content('啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊所得税')
/*
show_Msg('标题要长长长长的','这里可以写html比如加粗的的字体噢','删除',function(e){
 alert(e);
},'这里是点击删除后我传递过去的数据');
*/

function click_tr() {
 var checkbox = $("#mainData tr input");

 checkbox.eq(0).click(function() {
  //checkbox.attr('checked',$(this).get()[0].checked);

  bool = $(this).get()[0].checked;

  for (var i = 1; i    checkbox[i].checked = bool;
  };

 });

 $("#mainData tr").each(function(index) {
  $(this).click(function() {

   if (index == 0) {

   } else {
    var val = checkbox.eq(index);
        val = val.get()[0];
    var bool = val.checked;

    //alert(index);
    if (bool) {
     val.checked = false;
    } else {
     val.checked = true;
    }
   }

  });

 });
}index.html代码
[html]   
 

 
   
  无标题文档 
   
 
 
 
 
 
   
 
     
 
       

瀑布流留言板管理系统

 
     
 
   
 
 
   
 
       
 
     
 
 
       
 
     
 
   
 
 
 
 
 
 
 
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中