jquery取出checkbox后面的文本

theoffspring 2010-11-08 05:02:03
<td><input id= name="knowFrom" type="checkbox" value="" /> TV</td>

如何取出这个checkbox后面跟的文本?
...全文
2530 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
theoffspring 2010-11-16
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 babyboy9685 的回复:]
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
……
[/Quote]
解决了,this要用在jquery的写法时,不能直接使用,要$(this)才行,好奇怪呀。可以不必这么麻烦,像我这样写也可以:

<script type="text/javascript">
$(function(){
$("#_but").click(function(){
var checks = $("input[name='knowFrom']:checked");
$.each(checks,function(){
alert($(this).parent().text());
})
});

});
</script>
wangjianqiang24 2010-11-11
  • 打赏
  • 举报
回复
document.getElementById("id值")
逍遥庄主 2010-11-11
  • 打赏
  • 举报
回复

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Untitled Document</title>
<script language="JavaScript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#_but").click(function(){
var checks = $("input[name='knowFrom']:checked");
$.each(checks,function(idx,obj){
alert($(obj).parent("td").text());
})
});

});
</script>
</head>
<body>
<table>
<tr>
<td><input name="knowFrom" type="checkbox" value="1" />TV11</td>
</tr>
<tr>
<td><input name="knowFrom" type="checkbox" value="2" />TV2</td>
</tr>
<tr>
<td><input name="knowFrom" type="checkbox" value="3" />TV321</td>
</tr>
<tr>
<td><input name="knowFrom" type="checkbox" value="4" />TV4000</td>
</tr>
</table>
<input type="button" name="but" id="_but" value="取text">
</body>
</html>


这个效果么?
only_niu 2010-11-10
  • 打赏
  • 举报
回复
10楼正解
voice1122 2010-11-09
  • 打赏
  • 举报
回复
$("input[checked]").parent().text()
voice1122 2010-11-09
  • 打赏
  • 举报
回复
$("td").text()
lzh_me 2010-11-09
  • 打赏
  • 举报
回复


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Untitled Document</title>
<script language="JavaScript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(function(){
$("#_but").click(function(){
$("input[name='knowFrom'][checked]").each(function(){
alert($(this).parent().text());
});
});

});
</script>
</head>
<body>
<table>
<tr>
<td><input name="knowFrom" type="checkbox" value="1" />TV11</td>
</tr>
<tr>
<td><input name="knowFrom" type="checkbox" value="2" />TV2</td>
</tr>
<tr>
<td><input name="knowFrom" type="checkbox" value="3" />TV321</td>
</tr>
<tr>
<td><input name="knowFrom" type="checkbox" value="4" />TV4000</td>
</tr>
</table>
<input type="button" name="but" id="_but" value="取text">
</body>
</html>

zxqiangwhere 2010-11-09
  • 打赏
  • 举报
回复
获取checkbox父节点的text

$("input[checked]").parent().text()
OR
$("#knowFrom").parent().text()


theoffspring 2010-11-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 voice1122 的回复:]
$("input[checked]").parent().text()
[/Quote]
放在函数里调用不好用.
pm90125 2010-11-09
  • 打赏
  • 举报
回复
直接给个ID $(#ID名).text();不就有了么
voice1122 2010-11-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 theoffspring 的回复:]
引用 11 楼 voice1122 的回复:
$("input[checked]").parent().text()

放在函数里调用不好用.
[/Quote]

text()返回满足条件的所有文本,应该不用再写函数了吧
jhrxx 2010-11-09
  • 打赏
  • 举报
回复
爲什麽要把input跟文本放在一起,另外放個TD就好了
xiangwendong 2010-11-09
  • 打赏
  • 举报
回复

$("input[type='checkbox']).each(function(i){
$(this).innerHTML
})
theoffspring 2010-11-08
  • 打赏
  • 举报
回复
每个checkbox占用一个td
theoffspring 2010-11-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xiangwendong 的回复:]
document.getElementById("").innerHTML
[/Quote]
关键这不只是一个checkbox,是一组,你这个办法不行,即使只有一个,取出还要再做很多处理才能得到文本。
xiangwendong 2010-11-08
  • 打赏
  • 举报
回复
document.getElementById("").innerHTML
JAVA开发者OO 2010-11-08
  • 打赏
  • 举报
回复
可以把TV设置成checkbox这个标签的value值。。。。
theoffspring 2010-11-08
  • 打赏
  • 举报
回复
修正一下:

<td><input name="knowFrom" type="checkbox" value="" /> TV</td>



function getSelectedText(name) {
// if (2>1) return "";
var value = "";
$(":input[name='"+name+"']").filter("[checked=true]").each(function() {
var $obj=this;
if (this.checked) {
if (value == "") value += $obj.siblings().text();
else value += "-" + $obj.siblings().text();
}
});

return value;
}

用3楼的办法,在控制台中报错说siblings()不是函数.不能改html,因为定义了很多样式,修改成label的css可能失效,css是美工做的.
pxynet 2010-11-08
  • 打赏
  • 举报
回复
<td><input id= name="knowFrom" type="checkbox" value="" /> <label>TV</label></td>
$("#knowFrom").next().text()
shaoliang520xi 2010-11-08
  • 打赏
  • 举报
回复
$("#knowFrom").find("input[type='checkbox']:checked").siblings().text();
加载更多回复(2)

87,923

社区成员

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

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