[HTML]
<head runat="server">
<script type="text/javascript">
function chkvalue(theform) {
var listLen = theform.namedata.length; //計算 namedata 數量
var str= ""
for (var i = 0; i < listLen; i++) {
if (theform.namedata[i].checked) { //如果checked
intr += theform.namedata[i].value + " " //取值
}
}
if ( len(str) > 0 ) {
alert("checked value : " + intr)
}
}
function chkAll(tform) {
var listLen = tform.namedata.length;
for (var i = 0; i < listLen; i++) {
if (tform.CA.checked) {
tform.namedata[i].checked = true;
}
else {
tform.namedata[i].checked = false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="checkbox" name="namedata" value="Computing" />sunny<br />
<input type="checkbox" name="namedata" value="Music" />shine<br />
<input type="checkbox" name="namedata" value="Sports" />blin<br />
<input type="button" onclick="chkvalue(this.form)" value="Click me to see the result" />
<input type="button" onclick="this.form.namedata[2].checked = true" value="I like sports" />
<input type="button" onclick="this.form.namedata[2].click()" value="Toggle Sports checkbox" />
<input type="checkbox" name="CA" onclick="chkAll(this.form)" value="全選" />
</form>
</body>
</html>
PS. Asp.Net 元件,不能搭配 this.form 呼叫 function ,會失效
<head runat="server">
<script type="text/javascript">
function chkvalue(theform) {
var listLen = theform.namedata.length; //計算 namedata 數量
var str= ""
for (var i = 0; i < listLen; i++) {
if (theform.namedata[i].checked) { //如果checked
intr += theform.namedata[i].value + " " //取值
}
}
if ( len(str) > 0 ) {
alert("checked value : " + intr)
}
}
function chkAll(tform) {
var listLen = tform.namedata.length;
for (var i = 0; i < listLen; i++) {
if (tform.CA.checked) {
tform.namedata[i].checked = true;
}
else {
tform.namedata[i].checked = false;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="checkbox" name="namedata" value="Computing" />sunny<br />
<input type="checkbox" name="namedata" value="Music" />shine<br />
<input type="checkbox" name="namedata" value="Sports" />blin<br />
<input type="button" onclick="chkvalue(this.form)" value="Click me to see the result" />
<input type="button" onclick="this.form.namedata[2].checked = true" value="I like sports" />
<input type="button" onclick="this.form.namedata[2].click()" value="Toggle Sports checkbox" />
<input type="checkbox" name="CA" onclick="chkAll(this.form)" value="全選" />
</form>
</body>
</html>
PS. Asp.Net 元件,不能搭配 this.form 呼叫 function ,會失效
留言
張貼留言