jquery中對錶單的基本作業碼

來源:互聯網
上載者:User

複製代碼 代碼如下:<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="../conn/jsfile/jquery.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
文字框 :<input type="text" id="txt" value="txt" /><br />
選項按鈕:<input type="radio" value="男" checked="checked" name="sex" />男 <input type="radio" value="女" name="sex" checked="checked"/>女<br />
複選框:<input type="checkbox" value="1" name="cc"/>1<input type="checkbox" value="2" checked="checked" name="cc" />2<input type="checkbox" value="3" checked="checked" name="cc" />3<br />
下拉框:
<select id="sel" style="width: 100px">
<option value="yi">1</option>
<option value="er" selected="selected">2</option>
<option value="san">3</option>
</select>
<script language="javascript" type="text/javascript">
/*------------------------------------------------javascript-------------------------------------------*/
/* 擷取文字框的值
alert(document.getElementById("txt").value);
*/
/*擷取選項按鈕的值
var radios = document.getElementsByName("sex");
var n = radios.length;
for (var i = 0; i < n; i++) {
if (radios[i].checked) {
alert(radios[i].value);
}
}
*/
/*擷取複選框中選中的值的組合 1,2,3,4
var checkboxs = document.getElementsByName("cc");
var n = checkboxs.length;
for (var i = 0; i < n; i++) {
if (checkboxs[i].checked) {
alert(checkboxs[i].value);
}
}
*/
/*擷取下拉式清單的選中項的值
方法一:
alert(document.getElementById("sel").value);
方法二:
var sel = document.getElementById("sel");
alert(sel.options[sel.selectedIndex].value);
*/
/*------------------------------------------------jquery-------------------------------------------*/
/*擷取文字框的值
alert($("#txt").val());
*/
/*擷取單選框選中項的值
alert($(":radio[name='sex']:checked").val());
單選框賦值 設定選中項
$(":radio[name='sex']").val(["男"]);
*/
/*擷取複選框的值
$(":checkbox[name='cc']:checked").each(function(i) {
alert(this.value);
});
複選框賦值 選中這幾項
$(":checkbox[name='cc']").val(["1", "2", "3"]);
*/
/*擷取下拉式清單的值
alert($("#sel").val());
擷取下拉式清單的文本
alert($("#sel option:selected").text());
*/
</script>
</form>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.