60个很实用的jQuery代码开发技巧收集
发布时间:2016-11-23 20:36:04  所属栏目:Linux  来源:站长网 
            导读:由于内容比较多建议用CTRL+F搜索 偶然在网上看到这些不错的jQuery代码开发技巧。原文收集了30个,另外查找的时候发现了还有20个。加上另外十个实用的jQuery代码片段,共60个代码技巧,收集在一起分享给大家。 1. 创建一个嵌套的过滤器 .filter(":not(:has(
                
                
                
            | 大家可能在表单的操作中需要防止用户意外的提交表单,那么下面这段代码肯定非常有帮助: 
$("#form").keypress(function(e) {
 if (e.which == 13) {
 return false;
 }
});52: 清除所有的表单数据可能针对不同的表单形式,你需要调用不同类型的清楚方法,不过使用下面这个现成方法,绝对能让你省不少功夫。 
function clearForm(form) {
 // iterate over all of the inputs for the form
 // element that was passed in
 $(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
  this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
  this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
  this.selectedIndex = -1;
 });
};53: 将表单中的按钮禁用下面的代码对于ajax操作非常有用,你可以有效的避免用户多次提交数据,个人也经常使用: 禁用按钮: 
$("#somebutton").attr("disabled", true);启动按钮: 
$("#submit-button").removeAttr("disabled");可能大家往往会使用.attr(‘disabled',false);,不过这是不正确的调用。 54: 输入内容后启用递交按钮(编辑:南平站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 

