網站開發中常用js表單取值方法 收藏
一.常用表單基本取值方法(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
實現功能:遍歷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);
}
}
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/evenbing/archive/2008/11/24/3362662.aspx