Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript或Jquery选择GridView中选定的行项的子项?_Javascript_Jquery_Asp.net_Gridview_Asp.net 3.5 - Fatal编程技术网

如何使用JavaScript或Jquery选择GridView中选定的行项的子项?

如何使用JavaScript或Jquery选择GridView中选定的行项的子项?,javascript,jquery,asp.net,gridview,asp.net-3.5,Javascript,Jquery,Asp.net,Gridview,Asp.net 3.5,我在asp.net页面中有一个GridView,如下所示: Checkbox Name Link [] MainName myLink [] -somename1 myLink1 [] -somename2 myLink2 [] --somename2.1 myLink2.1 [] MainName2 mylink3 [] -somename3

我在asp.net页面中有一个GridView,如下所示:

Checkbox   Name          Link
[]         MainName      myLink
[]         -somename1    myLink1
[]         -somename2    myLink2
[]         --somename2.1 myLink2.1
[]         MainName2     mylink3
[]         -somename3    Mylink4
protected void SelectCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
    System.Web.UI.WebControls.CheckBox chk = sender as System.Web.UI.WebControls.CheckBox;
    GridViewRow row = (GridViewRow)chk.NamingContainer;
    bool checkVal = false;

    String cellText = row.Cells[2].Text;
    if (cellText.IndexOf('-') < 0)
    {
        foreach (GridViewRow r in dgMenuItems.Rows)
        {
            if (row == r)
            {
                checkVal = true;
            }

            if (checkVal)
            {
                if (r.Cells[2].Text.IndexOf('-') < 0)
                {
                    break;
                }
                else
                {
                    var cb = (System.Web.UI.WebControls.CheckBox)r.FindControl("chbSelect");
                    cb.Checked = true;
                }
            }

        }
    }
}
请注意,在命名模式中,“主标题”不包括破折号字符作为起始字符,而其他标题包含破折号字符

我想要的是,当我单击“MainName”附近的复选框时,我需要所有属于所选MainName的子行也将被自动选择。假设我单击第一行的复选框:MainName,那么应该会看到:

Checkbox          Name          Link
[checked]         MainName      myLink
[checked]         -somename1    myLink1
[checked]         -somename2    myLink2
[checked]         --somename2.1 myLink2.1
[]                MainName2     mylink3
[]                -somename3    Mylink4
我使用C#管理它,如下所示:

Checkbox   Name          Link
[]         MainName      myLink
[]         -somename1    myLink1
[]         -somename2    myLink2
[]         --somename2.1 myLink2.1
[]         MainName2     mylink3
[]         -somename3    Mylink4
protected void SelectCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
    System.Web.UI.WebControls.CheckBox chk = sender as System.Web.UI.WebControls.CheckBox;
    GridViewRow row = (GridViewRow)chk.NamingContainer;
    bool checkVal = false;

    String cellText = row.Cells[2].Text;
    if (cellText.IndexOf('-') < 0)
    {
        foreach (GridViewRow r in dgMenuItems.Rows)
        {
            if (row == r)
            {
                checkVal = true;
            }

            if (checkVal)
            {
                if (r.Cells[2].Text.IndexOf('-') < 0)
                {
                    break;
                }
                else
                {
                    var cb = (System.Web.UI.WebControls.CheckBox)r.FindControl("chbSelect");
                    cb.Checked = true;
                }
            }

        }
    }
}
protectedvoid SelectCheckBox\u一旦checkedchanged(对象发送方,事件参数e)
{
System.Web.UI.WebControls.CheckBox chk=发件人为System.Web.UI.WebControls.CheckBox;
GridViewRow行=(GridViewRow)chk.NamingContainer;
bool-checkVal=false;
字符串cellText=行。单元格[2]。文本;
if(cellText.IndexOf('-')小于0)
{
foreach(dgMenuItems.Rows中的GridViewRow r)
{
如果(行==r)
{
checkVal=true;
}
if(checkVal)
{
if(r.Cells[2].Text.IndexOf('-')<0)
{
打破
}
其他的
{
var cb=(System.Web.UI.WebControls.CheckBox)r.FindControl(“chbSelect”);
cb.Checked=真;
}
}
}
}
}

但是我想在JavaScript或JQuery中处理它,而不是在服务器端。在此方面的任何帮助或建议都将不胜感激。

这是一种方法

<script>
    $('.GridWithCheckBox input[type=checkbox]').change(function () {
        var $cb = $(this);

        //get the value of the cell to the right
        var $name = $cb.parent('td').next('td').html().trim();

        //check if the first value is not a '-'
        if ($name.substring(0, 1) !== '-') {

            //find all next rows and loop them
            $cb.closest('tr').nextAll().each(function () {

                //find the next cell value
                var $nextName = $(this).find('td:eq(1)').html().trim();

                //if it has a '-', check the checkbox
                if ($nextName.substring(0, 1) === '-') {
                    $(this).find('input[type=checkbox]').prop('checked', $cb.prop('checked'));
                } else {
                    //if not found stop the function
                    return false;
                }
            });
        }
    });
</script>