帮忙改段JS```谢谢啦...
这段JS主要实现了在树中选中一个子节点父节点不选中,当子节点全部被选中时父节点才选中.我现在希望改成选中一个子节点时父节点也选中,子节点一个未选中时父节点也是未选中.我JS很烂帮忙改下...
function yy_stv_foreachParentCheckbox(obj)
{
var checkedNum = 0;
var uncheckedNum = 0;
if (obj.tagName == "INPUT" && obj.type == "checkbox")
{
do
{
obj = obj.parentNode;
}
while (obj.tagName != "TABLE")
}
var tables = obj.parentNode.getElementsByTagName("TABLE");
if (typeof(tables) == 'undefined') return;
for (var i=0; i < tables.length; i++)
{
for (var x=0; x < tables[i].rows.length; x++)
{
for (var j=0; j < tables[i].rows[x].cells.length; j++)
{
var chk = tables[i].rows[x].cells[j].childNodes[0];
if (typeof(chk) != 'undefined' && chk.tagName == "INPUT" && chk.type == "checkbox")
{
if (chk.checked)
checkedNum ++;
else
uncheckedNum ++;
}
}
}
}
if (uncheckedNum == 0)
{
yy_stv_checkParentCheckbox(obj, true);
}
else
{
yy_stv_checkParentCheckbox(obj, false);
}
}
------解决方案--------------------
JScript code
function yy_stv_foreachParentCheckbox(obj)
{
var checkedNum = 0;
var uncheckedNum = 0;
if (obj.tagName == "INPUT" && obj.type == "checkbox")
{
do
{
obj = obj.parentNode;
}
while (obj.tagName != "TABLE")
}
var tables = obj.parentNode.getElementsByTagName("TABLE");
if (typeof(tables) == 'undefined') return;
for (var i=0; i < tables.length; i++)
{
for (var x=0; x < tables[i].rows.length; x++)
{
for (var j=0; j < tables[i].rows[x].cells.length; j++)
{
var chk = tables[i].rows[x].cells[j].childNodes[0];
if (typeof(chk) != 'undefined' && chk.tagName == "INPUT" && chk.type == "checkbox")
{
if (chk.checked)
checkedNum ++;
else
uncheckedNum ++;
}
}
}
}
if (checkedNum == 0)
{
yy_stv_checkParentCheckbox(obj, false);
}
else
{
yy_stv_checkParentCheckbox(obj, true);
}
}