请教一个JS函数!!

时光瞄 2009-12-03 01:18:35
<!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>
...全文
90 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangshaolongjj 2009-12-03
  • 打赏
  • 举报
回复
<body>
<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>
xmliy 2009-12-03
  • 打赏
  • 举报
回复
使用jQuery


function func()
{
var checkedText = '';
$('input:checked').each(function()
{
checkedText += (checkedText == '' ? '' : ',') + $.trim(this.nextSibling.nodeValue);
});
alert(checkedText);
}
cntmi 2009-12-03
  • 打赏
  • 举报
回复
<!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() {
//如果获取选中复选框的文本值
//比如我选中了第一个和第三个框
//如何获得“文本一文本三”
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>
crazylaa 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wcwtitxu 的回复:]
HTML code<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><title> new document</title><metaname="generator" content="editplus"/><metaname="author" content=""/><metaname="keywords" content=""/><metaname="description" content=""/><scripttype="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); });returnthis; };
Array.prototype.each=function(trans) {returnthis.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() {returnthis.replace(/^\s+|\s+$/g,''); };function fun() {var checkboxes= Array.prototype.collect.call(document.getElementsByName("chk1"),function(){returntrue; });var texts= checkboxes.collect(function(checkbox) {return checkbox.checked;
}).map(function(checkbox) {return checkbox.nextSibling.nodeValue.trim();
});
alert(texts.join("|"));
}</script></head><body><inputtype="checkbox" name="chk1"/> 文本1<inputtype="checkbox" name="chk1"/> 文本2<inputtype="checkbox" name="chk1"/> 文本3<inputtype="button" value="CLICK" onclick="fun();"/></body></html>
[/Quote]

mark
wcwtitxu 2009-12-03
  • 打赏
  • 举报
回复
<!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">
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>

87,902

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧