<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript">
function fun() {
//如果获取选中复选框的文本值
//比如我选中了第一个和第三个框
//如何获得“文本一文本三”
}
</script>
</head> <body>
<input type="checkbox" name="chk1"/> 文本1
<input type="checkbox" name="chk1"/> 文本2
<input type="checkbox" name="chk1"/> 文本3
<input type="button" value="CLICK" onclick="fun();"/>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript">
function fun() {
//如果获取选中复选框的文本值
//比如我选中了第一个和第三个框
//如何获得“文本一文本三”
}
</script>
</head> <body>
<input type="checkbox" name="chk1"/> 文本1
<input type="checkbox" name="chk1"/> 文本2
<input type="checkbox" name="chk1"/> 文本3
<input type="button" value="CLICK" onclick="fun();"/>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript">
Array.prototype.collect = function(selector, limit) {
var r = [];
for (var i=0, len=this.length, limit=limit||len; i<len && r.length<limit; i++)
if (!!selector(this[i], i, this))
r.push(this[i]);
return r;
};
Array.prototype.forEach = function(action) { this.collect(function(o, i, a) { action(o, i, a); }); return this; };
Array.prototype.each = function(trans) { return this.forEach(function(o, i, a) { a[i] = trans(o, i, a); }); }
Array.prototype.map = function(trans) { return [].concat(this).each(trans); };
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };function fun() {
var checkboxes = Array.prototype.collect.call(document.getElementsByName("chk1"), function(){ return true; });
var texts = checkboxes.collect(function(checkbox) {
return checkbox.checked;
}).map(function(checkbox) {
return checkbox.nextSibling.nodeValue.trim();
});
alert(texts.join("|"));
}
</script>
</head> <body>
<input type="checkbox" name="chk1"/> 文本1
<input type="checkbox" name="chk1"/> 文本2
<input type="checkbox" name="chk1"/> 文本3
<input type="button" value="CLICK" onclick="fun();"/>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript">
function fun() {
//如果获取选中复选框的文本值
//比如我选中了第一个和第三个框
//如何获得“文本一文本三”
var str = "" ;
var checks = document.getElementsByName("chk1");
for(var i=0 ; i < checks.length ; i++){
if(checks[i].checked){
str += checks[i].value+"," ;
}
}
alert(str.substring(0,str.length-1));
}
</script>
</head> <body>
<input type="checkbox" name="chk1" value="文本1"/> 文本1
<input type="checkbox" name="chk1" value="文本2"/> 文本2
<input type="checkbox" name="chk1" value="文本3"/> 文本3
<input type="button" value="CLICK" onclick="fun();"/>
</body>
</html>
function func()
{
var checkedText = '';
$('input:checked').each(function()
{
checkedText += (checkedText == '' ? '' : ',') + $.trim(this.nextSibling.nodeValue);
});
alert(checkedText);
}
<span><input type="checkbox" name="chk1"/> 文本1</span>
<span><input type="checkbox" name="chk1"/> 文本2</span>
<span><input type="checkbox" name="chk1"/> 文本3</span>
<input type="button" value="CLICK" onclick="fun();"/>
</body>
<script type="text/javascript">
function fun(){
var dd = document.getElementsByName("chk1");
for(var i=0;i<dd.length;i++){
if(dd[i].checked==true){
alert(dd[i].parentNode.innerText)
}
}
}
</script>