选中列中的所有复选框,并在检查其他列时取消选中它们

时间:2017-10-25 16:08:21

标签: javascript jquery html

我一直无法完成这项工作。我有两列复选框,每列有一个复选框,可以选择所有其他复选框。

Example

问题是,当我选择其他列时,子复选框保持选中状态

Example 2

我正在使用下一个jquery函数使其工作,但它只是部分工作

选择所有复选框的脚本:

Sub login()
    Dim objIE As InternetExplorer
    Dim uid As String
    Dim pwd As String
    Dim rng As Range
    Dim sh As Worksheet
    Dim objCollection As Object
    Dim buttonCollection As Object
    Dim ieElement As Object
    Dim ieButton As Object
    Dim WinHttpReq As Object
    Dim oStream As Object
    Dim MyURL As String

    Application.ScreenUpdating = False

    Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
    Set sh = Sheets("Indeed Resume Download")
    Set rng = sh.Range("A2")

    If Trim(rng.Value) = "" Or Trim(rng.Offset(0, 1).Value) = "" Then
        MsgBox "User ID And Password are mandatory."
        Exit Sub
    End If

    On Error Resume Next
    objIE.Quit
    Set objIE = Nothing
    On Error GoTo 0

    uid = rng.Value
    pwd = rng.Offset(0, 1).Value

    Dim j As Long

    Set objIE = New InternetExplorer 'Initialize internet object
    objIE.Navigate "https://secure.indeed.com/account/login?service=my&hl=en_IN&co=IN&continue=https%3A%2F%2Fwww.indeed.co.in%2F"
    objIE.Visible = True

    With objIE
        Do While .Busy: Loop
        Do While .ReadyState <> READYSTATE_COMPLETE: Loop
        Do While .Busy: Loop
    End With

    objIE.Document.all.signin_email.Value = uid
    objIE.Document.all.signin_password.Value = pwd

    Set ieElement = objIE.Document.getElementsByClassName("sg-btn sg-btn-primary btn-signin")(0)

    ieElement.Click

    For j = 2 To sh.Cells(Rows.Count, 27).End(xlUp).Row

        Set objIE = New InternetExplorer
        objIE.Visible = True

        Dim Sender As String
        MyURL = sh.Range("XDA" & j).Value
        Sender = sh.Range("XDB" & j).Value

        objIE.Navigate MyURL

        Do While objIE.Busy = True
            DoEvents
        Loop

        Do While objIE.Busy: Loop
        Do While objIE.ReadyState <> READYSTATE_COMPLETE: Loop
        Do While objIE.Busy: Loop

        On Error Resume Next
        Set ieButton = objIE.Document.getElementsByClassName("button download_button")(0)

        ieButton.Click

        WinHttpReq.Open "GET", MyURL, False
        WinHttpReq.Send

        MyURL = WinHttpReq.responseBody
        Set oStream = CreateObject("ADODB.Stream")
        oStream.Open
        oStream.Type = 1
        oStream.Write WinHttpReq.responseBody
        oStream.Save ("D:\Downloaded Indeed Resume - Store")
        'oStream.SaveToFile ("D:\Downloaded Indeed Resume - Store\" & Sender & ".pdf"), adSaveCreateOverWrite
        oStream.Close

    Next j    
    Set objIE = Nothing

End Sub

脚本只选择所需列中的复选框:

$("th input[type='checkbox']").on("change", function () {
    debugger;
    var cb = $(this),          //checkbox that was changed
        th = cb.parent(),      //get parent th
        col = th.index() + 1;  //get column index. note nth-child starts at 1, not zero
    $("tbody td:nth-child(" + col + ") input").prop("checked", this.checked);  //select the inputs and [un]check it
});

HTML:

 $('table').attr('id', 'test');

    $('input[type="checkbox"]').on('change', function () {
        var checado = $(this).prop('checked');

        $(this).closest('tr').find('input[type="checkbox"]').each(function () {
            $(this).prop('checked', false);
        });
        $(this).prop("checked", checado);
    });

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以先取消选中所有复选框(当前点击的除外)然后检查所需的复选框:

MongoTemplate

希望这有帮助。

&#13;
&#13;
//Uncheck all the checkboxes except the current clicked
$("table input:checkbox").not(this).prop("checked", false);
&#13;
$("th input[type='checkbox']").on("change", function() {
  debugger;

  //Uncheck all the checkboxes except the current clicked
  $("table input:checkbox").not(this).prop("checked", false);

  var cb = $(this), //checkbox that was changed
    th = cb.parent(), //get parent th
    col = th.index() + 1; //get column index. note nth-child starts at 1, not zero

  $("tbody td:nth-child(" + col + ") input").prop("checked", this.checked); //select the inputs and [un]check it
});
&#13;
&#13;
&#13;