常用的JavaScript或jQuery对form表单的操作代码总结
1.javascript去空格 function trim(text) { return(text||"").replace(/^s+|s+$/g,""); } 2.检查复选框是否选值var count=document.form.role.length; varj=0; for(vari=0;i<count;i++) { if(document.form.role[i].checked) ++j; } if(j==0) { alert("请选择角色");returnfalse; } 3.使按钮变灰 <INPUTTYPE="button"value="变灰"name=buttononclick="javascript:aa.disabled=true;bb.disabled=true;"> <INPUTTYPE="button"value="变亮"name=buttononclick="javascript:aa.disabled=false;bb.disabled=false;"> 循环变灰或变亮(bu1为按钮id) functionturndisable(){ bu1.disabled=!bu1.disabled; } 4.js往空白的select列表框中添加值//select 添加 //obj1为用于输入添加值的input域id,obj2为空白的多选列表框id functionfunAdd(obj1,obj2){ varobjLeft=document.getElementById(obj1); varobj=trim(objLeft.value); if(obj==""){ alert("不能添加空值"); returnfalse; } varobjRight=document.getElementById(obj2); varvalue=newOption(obj,obj); objRight.options[objRight.length]=_value; objLeft.value=""; } //selsect删除 functionfunDelete(obj){ varobjRight=document.getElementById(obj); for(vari=0;i<objRight.length;i++){ if(objRight[i].selected==true){ objRight.options.remove(objRight.selectedIndex); } } }5.js控制的多选列表框提交值到后台(即上面4的空白select添加完值后,提交)。 //此JavaScript为提交两个select(id分别是phonelist,phonelist1)多选列表框 functionaddList(){ varlist=document.getElementById('list'); varphonelist=document.getElementById('phonelist'); varphonelist1=document.getElementById('phonelist1'); varhtml=''; for(vari=0;i<phonelist.length;i++){ html+='<inputtype="hidden"value="'+phonelist[i].value+'"name="phone"/>'; } for(varj=0;j<phonelist1.length;j++){ html+='<inputtype="hidden"value="'+phonelist1[j].value+'"name="black"/>'; } //list为div层的id list.innerHTML=html; document.form1.action="pew.do?method=ModifyPaw"; document.form1.submit(); } 6.多选列表框,把左边列表中的值,转移到右边的空白多选列表框然后提交 把左边的列表值转移到右边的JavaScript为// obj1 ,obj2为左右select多选列表框id //添加 functionfunAdd(obj1,obj2){ varobjLeft=document.getElementById(obj1); varobjRight=document.getElementById(obj2); for(vari=0;i<objLeft.length;i++){ if(objLeft[i].selected==true){ //alert(objLeft[i].value); varvalue=newOption(objLeft[i].text,objLeft[i].value); objRight.options[objRight.length]=_value; objLeft.options.remove(objLeft.selectedIndex); } } } //移除 functionfunDelete(obj1,obj2){ varobjLeft=document.getElementById(obj1); varobjRight=document.getElementById(obj2); for(vari=0;i<objRight.length;i++){ if(objRight[i].selected==true){ var_value=newOption(objRight[i].text,objRight[i].value); objLeft.options[objLeft.length]=_value; objRight.options.remove(objRight.selectedIndex); } } } //全部移除 functionAllDelete(obj1,obj2){ varobjLeft=document.getElementById(obj1); varobjRight=document.getElementById(obj2); for(vari=0;i<objRight.length;i++){ var_value=newOption(objRight[i].text,objRight[i].value); objLeft.options[objLeft.length]=_value; } objRight.innerHTML=""; } //全部加入 functionAllAdd(obj1,obj2){ varobjLeft=document.getElementById(obj1); varobjRight=document.getElementById(obj2); for(vari=0;i<objLeft.length;i++){ var_value=newOption(objLeft[i].text,objLeft[i].value); objRight.options[objRight.length]=_value; } objLeft.innerHTML=""; } 7. Ajax 从后台提取信息 把值放入到select多选列表框中//ajax提取修改信息; function updateShow(id){ $.ajax({ url:'${pageContext.request.contextPath}/pew.do?method=ajaxGetPaw', type:'post', dataType:'xml', data:'pewid='+id, error:function(json){ alert("数据读取错误!"); }, success:function(xml){ /×*******************赋值到禁播电话select中去***************************/ $("#phonelist1").html(""); //取到xmlphone集合 varobjRight=document.getElementById('phonelist1'); $(xml).find("black").each(function(){ varvalue=newOption($(this).text(),$(this).text()); objRight.options[objRight.length]=_value; }); /×***************赋值到黑名单select中去*******************/ $("#phonelist").html(""); varobjRight1=document.getElementById('phonelist'); $(xml).find("phone").each(function(){ varvalue=newOption($(this).text(),$(this).text()); objRight1.options[objRight1.length]=_value; }); } }); } 8.JavaScript 或jQuery 取值赋值例子 总结//input 域赋值 $("#company").val('${company}'); //从XML中取值 $(xml).find("code").text() //html标签赋值code为<tdid="code"></td> $("#code").html($(xml).find("code").text()); //往一个td标签中替换值name为参数 document.getElementById('namej').innerHTML=name; attrtext 9.jQueryAjax提交 functionshowFileInfo(id){ $.ajax({ url:'${pageContext.request.contextPath}/user.do?method=getFileInFo', type:'post', dataType:'xml', data:'id='+id, error:function(json){ alert("没有选择档案号!"); }, success:function(xml){ $(xml).find("name").each(function(){ varid=$(this).attr("id"); varname=$(this).text(); }); //往HTML标签中赋空值 $("#eName").html(""); //往HTML标签中追加值 $("#eName").append($(xml).find("cName").text()); }} )}10.Ajax提交form表单 需引入jquery.form.js $(function(){ $("#sub").click(function(){ $("#form").attr("action","${pageContext.request.contextPath}/user.do?method=toModifyUserArchiveRight"); if(addList()==false){ returnfalse; } $("#form").submit(); }); $("#form").submit(function(){ $(this).ajaxSubmit({ type:"post", clearForm:true, success:showResponse }); returnfalse; }); functionshowResponse(responseText,statusText){ if(statusText){ alert("操作成功!"); } else{ alert("操作失败!"); } } }); 11.给单选框赋值 varstate=document.form1.state for(vari=0;i<state.length;i++){ if(state[i].value==$(xml).find("state").text()){ state[i].checked=true; } } $("#stateoption[value='${state1}']").attr("selected","selected"); 12.给复选框赋值 varrole=document.form1.role for(vark=0;k<role.length;k++){ role[k].checked=false; } $(xml).find("role").each(function(l){ for(vari=0;i<role.length;i++){ if(role[i].value==$(this).text()){ role[i].checked=true; } } }) 12.给下拉菜单赋值与取值//赋值 $("#statis1option[value='company']").attr("selected","selected"); //取值 $('#statis1option:selected').val(); $('#statis1option:selected').text(); //取消下拉菜单中其中一个值 $("#statis1option[value='company']").remove(); 13.判断页面中是否有某个DOM对象if(typeof(eval(document.getElementsByName('ersuser')))== "undefined") { alert("没有这个对象"); returnfalse; } |