一.常用表單基本取值方法(form1為表單名稱,TextBox1為控件ID,以文本框為例,html控件與web服務器控件是一樣的)
1.form1.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
實現(xiàn)功能:遍歷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
實現(xiàn)功能:遍歷html復選框,得到所選中項
代碼同html復選框
3.html下拉列表框
表單:<select id="Select1" multiple>
<option value=1>1</option>
<option value=2>2</option>
</select>
實現(xiàn)功能:
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控件
實現(xiàn)功能:得到所選中項的text和value值
代碼同html下拉列表框
5.CheckBoxList控件
實現(xiàn)功能:得到所選中項的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);
}
}
本文來自: PQ秀秀網(wǎng)(
http://www.pqshow.com) 詳細出處參考:
http://www.pqshow.com/program/aspnet/200909/9646.html
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請
點擊舉報。