10个超棒jQuery表单操作代码片段
http://www.qianduan.net/10-awesome-jquery-form-action-code-fragment-not-to-be-missed.html
jQuery绝对是一个伟大的开源javascript类库,是帮助我们快速和高效开发前端应用的利器。可能大家在日常的开发过程中常常会处理表单相关的javascript,在今天这篇代码片段分享文章中,这里收集了10个超棒超实用的jQuery表单处理代码,希望能够在大家的开发过程中帮助大家更好更快的处理表单相关问题,希望大家喜欢!如果你也有相关的代码,请大家积极分享!
代码片段1: 在表单中禁用“回车键”
大家可能在表单的操作中需要防止用户意外的提交表单,那么下面这段代码肯定非常有帮助:
$("#form").keypress(function(e) { if (e.which == 13) { return false; }});
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; });};
$("#somebutton").attr("disabled", true);
$("#submit-button").removeAttr("disabled");
$('#username').keyup(function() { $('#submit').attr('disabled', !$('#username').val()); });
$(document).ready(function() { $('form').submit(function() { if(typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') { jQuery.data(this, "disabledOnSubmit", { submited: true }); $('input[type=submit], input[type=button]', this).each(function() { $(this).attr("disabled", "disabled"); }); return true; } else { return false; } });});
$("form :input").focus(function() { $("label[for='" + this.id + "']").addClass("labelfocus");}).blur(function() { $("label").removeClass("labelfocus");});
//change event on password1 field to prompt new input$('#password1').change(function() { //dynamically create new input and insert after password1 $("#password1").append("<input type='text' name='password2' id='password2' />");});
$(function(){ $("select#ctlJob").change(function(){ $.getJSON("/select.php",{id: $(this).val(), ajax: 'true'}, function(j){ var options = ''; for (var i = 0; i < j.length; i++) { options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'; } $("select#ctlPerson").html(options); }) })})
$('#checkBox').attr('checked');
$("#myform").submit();