如何在javascript中识别单选按钮
我正在创建一个动态表,其中每一行都有一组单选按钮放在不同的列中。 我想在单击单选按钮时更改单元格背景颜色。我正在使用下面的代码片段构建表。如何在javascript中识别单选按钮,以便我可以使用适当的背景颜色设置单元格如何在javascript中识别单选按钮,javascript,html,jsp,jstl,Javascript,Html,Jsp,Jstl,我正在创建一个动态表,其中每一行都有一组单选按钮放在不同的列中。 我想在单击单选按钮时更改单元格背景颜色。我正在使用下面的代码片段构建表。如何在javascript中识别单选按钮,以便我可以使用适当的背景颜色设置单元格 <table> <c:forEach> <tr> <c:forEach> <td><input type="radio" value=
<table>
<c:forEach>
<tr>
<c:forEach>
<td><input type="radio" value="" /></td>
</c:forEach>
</tr>
</c:forEach>
</table>
您应该在创建输入字段时尝试为其设置一个唯一的id(例如id=“theradiobutton”)
然后您可以使用DOM方法轻松地引用它 您需要设置id或使用假类名 稍后我应该使用jquery来访问和更改它们。
var myform=document.forms['myform'];
对于(var i=0;i
希望有帮助。使用dojo或jQuery选择radioButton节点,然后使用CSS过滤器表达式将td标记(parentNode dom节点)设置为您想要的任何颜色 使用dojo的示例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dojo selectors example</title>
<script type="text/javascript">
var djConfig = {
parseOnLoad: true,
isDebug: true,
locale: 'en-us'
};
</script>
<script type="text/javascript" src="http://o.aolcdn.com/dojo/1.4.1/dojo/dojo.xd.js"></script>
<script type="text/javascript">
dojo.addOnLoad(function() {
// Get all the radioButtons that are inside a <td> tag
dojo.query("td > input").forEach(function(node, index, array){
var td = node.parentNode;
dojo.addClass(td, "red");
dojo.connect(td, "onclick", function(){dojo.toggleClass(td, "white");});
});
});
</script>
<style type="text/css">
.red { background-color : red; }
.white { background-color : white; }
</style>
</head>
<body>
<form id="form1">
<table>
<tr>
<td><input type="radio" value="" /></td>
</tr>
<tr>
<td><input type="radio" value="" /></td>
</tr>
<tr>
<td><input type="radio" value="" /></td>
</tr>
</table>
</form>
</body>
</html>
Dojo选择器示例
var djConfig={
parseOnLoad:true,
是的,
地点:“en us”
};
dojo.addOnLoad(函数(){
//获取标签中的所有单选按钮
forEach(函数(节点、索引、数组){
var td=node.parentNode;
dojo.addClass(td,“红色”);
connect(td,“onclick”,function(){dojo.toggleClass(td,“white”);});
});
});
.red{背景色:红色;}
.white{背景色:白色;}
我将由您来纠正radiobutton的行为…这很简单。这是一个,复制,粘贴,运行它
<!doctype html>
<html lang="en">
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
$('#tableId input[name=row]').click(function() {
$('#tableId tr').removeClass('selected'); // Reset.
$(this).parents('tr').addClass('selected');
});
});
</script>
<style>
tr.selected {
background: #ffc;
}
</style>
</head>
<body>
<table id="tableId">
<tr><td><input type="radio" name="row" value="row1">row1</td></tr>
<tr><td><input type="radio" name="row" value="row2">row2</td></tr>
<tr><td><input type="radio" name="row" value="row3">row3</td></tr>
</table>
</body>
</html>
试验
$(文档).ready(函数(){
$(“#tableId输入[name=row]”)。单击(函数(){
$('#tableId tr')。removeClass('selected');//重置。
$(this).parents('tr').addClass('selected');
});
});
选定{
背景:#ffc;
}
第1行
第2行
第3行
只需将该表转换回您使用JSP/JSTL时的动态风格。不必过于挑剔,这非常简单。您不需要标识单选按钮,只需调用事件处理程序并将按钮实例与之一起传递:
<td><input type="radio" value="" onclick="colorMyCell(this)" /></td>
和处理程序:
function colorMyCell(inp) {
// get reference to the row
var tr = inp.parentNode.parentNode;
// put the TD children of the row into an array
var cells = tr.getElementsByTagName("TD");
// bgcolor all the other cells in that row white
for (var i=0; i<cells.length; i++) {
cells[i].style.backgroundColor = "#ffffff";
}
// now bgcolor the selected cell differently
inp.parentNode.style.backgroundColor = "#ffffcc";
}
函数colorMyCell(inp){
//获取对该行的引用
var tr=inp.parentNode.parentNode;
//将该行的TD子项放入数组中
var cells=tr.getElementsByTagName(“TD”);
//BG将该行中的所有其他单元格都涂成白色
对于(var i=0;这是可以的,但是如果您有多个单选按钮,并且只想在其中一个按钮上设置背景色,那么它将开始变得混乱…您还可以添加一个onClick()事件设置为每个单选按钮,以执行您所需的操作。但最好有一个唯一的标识符…这是我将采用的方法,但我将使用内置的jquery单选选择器,因为它性能更高(并且需要更少的html调整)。因此它将是$(“#tableId:radio”)。单击(function()…)@ckramer:我知道这个。但是,他可能在同一张表中有更多的单选按钮用于其他目的。否则我会“意外”使用
input[type=radio]
;)哦,如果你喜欢这种方法,为什么不直接向上投票呢
function colorMyCell(inp) {
// get reference to the row
var tr = inp.parentNode.parentNode;
// put the TD children of the row into an array
var cells = tr.getElementsByTagName("TD");
// bgcolor all the other cells in that row white
for (var i=0; i<cells.length; i++) {
cells[i].style.backgroundColor = "#ffffff";
}
// now bgcolor the selected cell differently
inp.parentNode.style.backgroundColor = "#ffffcc";
}