10个实用的jQuery表单操作代码片段.docVIP

  • 4
  • 0
  • 约 5页
  • 2016-12-27 发布于重庆
  • 举报
jQuery 绝对是一个伟大的开源JavaScript类库,是帮助我们快速和高效开发前端应用的利器。可能大家在日常的开发过程中常常会处理表单相关的 JavaScript,在今天这篇代码片段分享文章中,我们收集了10个超棒超实用的jQuery表单处理代码,希望能够在大家的开发过程中帮助大家更好更快的处理表单相关问题,希望大家喜欢! 1.? 在表单中禁用“回车键” 大家可能在表单的操作中需要防止用户意外的提交表单,那么下面这段代码肯定非常有帮助: Javascript代码 $(#form).keypress(function(e)?{ ?? ??if?(e.which?==?13)?{ ?? ????return?false; ?? ??} ?? });?? $(#form).keypress(function(e) { if (e.which == 13) { return false; } }); 在线调试 / 在线演示 2.? 清除所有的表单数据 可能针对不同的表单形式,你需要调用不同类型的清楚方法,不过使用下面这个现成方法,绝对能让你省不少功夫。 Javascript代码 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 ?? ????//?its?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; ?? ??}); ?? };?? 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 // its 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 elemen

文档评论(0)

1亿VIP精品文档

相关文档