Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用select change提高复选框attr的ajax切换性能_Jquery_Ajax - Fatal编程技术网

Jquery 使用select change提高复选框attr的ajax切换性能

Jquery 使用select change提高复选框attr的ajax切换性能,jquery,ajax,Jquery,Ajax,我正在创建一个简单的消息系统,带有类似于邮件收件箱的UI。我有一个选择框,它可以控制每个消息的一个复选框列表。它工作得很好,但我需要更少的代码行 这是我的密码: jQuery("#select_deselect").change(function(){ switch (jQuery(this).val()){ case 'unread': jQuery("input[class=new_message]").each(function(){ jQuery(this).

我正在创建一个简单的消息系统,带有类似于邮件收件箱的UI。我有一个选择框,它可以控制每个消息的一个复选框列表。它工作得很好,但我需要更少的代码行

这是我的密码:

jQuery("#select_deselect").change(function(){
  switch (jQuery(this).val()){
   case 'unread':
    jQuery("input[class=new_message]").each(function(){
     jQuery(this).attr("checked", "checked");
    });
    jQuery("input[class=message]").each(function(){
     jQuery(this).removeAttr("checked");
    });
   break;
   case 'read':
    jQuery("input[class=message]").each(function(){
     jQuery(this).attr("checked", "checked");
    });
    jQuery("input[class=new_message]").each(function(){
     jQuery(this).removeAttr("checked");
    });
   break;
   case 'all':
    jQuery("input[class=new_message]").each(function(){
     jQuery(this).attr("checked", "checked");
    });
    jQuery("input[class=message]").each(function(){
     jQuery(this).attr("checked", "checked");
    });
   break;
   case 'none':
     jQuery("input[class=new_message]").each(function(){
     jQuery(this).removeAttr("checked");
    });
    jQuery("input[class=message]").each(function(){
     jQuery(this).removeAttr("checked");
    });
   break;
   default:
    jQuery("input[class=new_message]").each(function(){
     jQuery(this).removeAttr("checked");
    });
    jQuery("input[class=message]").each(function(){
     jQuery(this).removeAttr("checked");
    });
   }
 });
select_deselect是我的选择框的id,带有选项(null、all、read、unread、none)

new_message是指定给未读邮件的类

message是指定用于读取消息的类

我认为toggle()可以做到这一点,但它将是相同的行


如果想简单地实现这一点

无需在选择每个元素后循环它们,只需调用.attr()或.removeAttr()


我通过删除冗余的每个,并将所有和无的选择器组合在一起,简化了上述操作。

仅在每次更改事件中缓存它们,但如果您做的更多,只需更改此函数中的选中值,则会有所帮助。这是一个小优化,我将使用局部变量存储所有输入,以提高每种情况下的速度。正确的bgy。。凯尔。。你的优化稍微慢了一点,但谢谢你,我会把它留到其他活动中。
jQuery("#select_deselect").change(function(){
    var newmessage = jQuery("input[class=new_message]");
    var message = jQuery("input[class=message]");
    switch (jQuery(this).val()){
        case 'unread':
            newmessage.attr("checked", "checked");
            message.removeAttr("checked");
        break;
        case 'read':
            message.attr("checked", "checked");
            newmessage.removeAttr("checked");
        break;
        case 'all':
            newmessage.attr("checked", "checked");
            message.attr("checked", "checked");
        break;
        case 'none':
        default:
            newmessage.removeAttr("checked");
            message.removeAttr("checked");
    }
});
jQuery("#select_deselect").change(function(){
  switch (jQuery(this).val()){
   case 'unread':
    jQuery("input[class=new_message]").attr("checked", "checked");
    jQuery("input[class=message]").removeAttr("checked");
   break;
   case 'read':
    jQuery("input[class=message]").attr("checked", "checked");
    jQuery("input[class=new_message]").removeAttr("checked");
   break;
   case 'all':
    jQuery("input[class=message],input[class=new_message]").attr("checked", "checked");
   break;
   case 'none':
   default:
     jQuery("input[class=message],input[class=new_message]").removeAttr("checked");
   break;
   }
 });