表單是那麼重要,它是人機交互的藉口,絕大多數的數據採集都要靠表單來完成。表單元素有很多,javascript提供了操縱表單元素的很多方法。下面一一介紹。
一:文本輸入域<input type="text" />
<input type="text" id="username" name="username" value="默認值" size='10' maxLength="5"/>
var textnode = document.getElementById("username");
設置禁用:textnode.disabled = true;
取消禁用:textnode.disabled = false;
設置只讀:textnode.readOnly = true;
取消只讀:textnode.readOnly = false;
聚集焦點:textnode.focus();
失去焦點:textnode.blur();
二:按鈕<input type="button"/>
<input type="button" id="mybutton" value="單擊我" onclick="show()"/>
var btnnode = document.getElementById("mybutton");
設置禁用:btnnode.disabled = true;
取消禁用:btnnode.disabled = false;
聚集焦點:btnnode.focus();
失去焦點:btnnode.blur();
三:單選按鈕<input type="radio"/>
<input type="radio" name='sex' value='m' onclick="check(this.value)"/>男
<input type="radio" name='sex' value='f' onclick="check(this.value)"/>女
設置禁用:radionode.disabled = true;
取消禁用:radionode.disabled = false;
聚集焦點:radionode.focus();
失去焦點:radionode.blur();
四:下拉按鈕<select>
<select id="myselect">
<option value='1' slected>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>