龙岩易富通网络科技有限公司

龙岩小程序开发,龙岩分销系统

常用Javascript表单取值方法

2015.09.10 | 991阅读 | 0条评论 | 通用代码

一.常用表单基本取值方法(frm为表单名称,TextBox1为控件ID,以文本框为例,html控件与web服务器控件是一样的)


 1.frm.TextBox1.value

 2.var txt = document.all.TextBox1;

 txt.value

 3.var txt = document.all["TextBox1"];

 txt.value

 4.document.getElementById("TextBox1");


二.


1.html复选框(name相同)


表单: <input id="Checkbox1" type="checkbox" name="chk" value="123" >sss


 <input id="Checkbox2" type="checkbox" name="chk" value="456">aaa


 <input id="Checkbox3" type="checkbox" name="chk" value="789">bbb


实现功能:遍历html复选框,得到所选中项


var oChks = document.all.chk;


 for(var i=0; i<oChks.length; i++)

 {

 if(oChks[i].checked)

    alert(oChks[i].value);

 }


2.html单选框(name相同)


表单: <input id="Radio1" type="radio" name="rad" value="123">123


 <input id="Radio2" type="radio" name="rad" value="456">456


实现功能:遍历html复选框,得到所选中项


代码同html复选框


3.html下拉列表框


表单:<select id="Select1" multiple>

 <option value=1>1</option>

 <option value=2>2</option>

 </select>


实现功能:


 3.1得到所选中项的text和value值(选择一项)


 var selDrp = document.all.Select1;

 alert(selDrp.options[selDrp.selectedIndex].text);

 alert(selDrp.options[selDrp.selectedIndex].value);


 3.2得到所选中项的text和value值(选择多项)


 for(var j=0;j<selDrp.options.length;j++)

 {

 if(selDrp.options[j].selected)

 {

 alert(selDrp.options[j].value);

 }

 }


4.DropDownList控件与ListBox控件


实现功能:得到所选中项的text和value值


代码同html下拉列表框


5.CheckBoxList控件


实现功能:得到所选中项的text


代码:


 var chklist = document.all("CheckBoxList1");

 var i = 0;

 for(i=0;i<chklist.rows.length;i++)

 {

 var name = "CheckBoxList1_" + i;

 var tmpChecked = document.all[name].checked;

 if(tmpChecked)

 {

 alert(document.all[name].parentElement.innerText);

 }

 }




function clearMateriel(obj)

 {

//全部选中

 if(obj=="all")

 {

 for(var i=0;i<document.getElementById("cblMateriel").getElementsByTagName("input").length;i++)

 {

 document.getElementById("cblMateriel_"+i).checked =true;

 }

 }

//清除

else if(obj=="none")

 {

 for(var i=0;i<document.getElementById("cblMateriel").getElementsByTagName("input").length;i++)

 {

 document.getElementById("cblMateriel_"+i).checked =false;

 }

 }

//反选

 else if(obj=="reverse")

 {

 for(var i=0;i<document.getElementById("cblMateriel").getElementsByTagName("input").length;i++)

 {

 if(document.getElementById("cblMateriel_"+i).checked)

 document.getElementById("cblMateriel_"+i).checked =false;

 else

 document.getElementById("cblMateriel_"+i).checked =true;

 }

 }

 }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yanleigis/archive/2008/11/11/3265656.aspx">http://blog.csdn.net/yanleigis/archive/2008/11/11/3265656.aspx


赞 (

发表评论