简体   繁体   English

javascript复选框选中所有

[英]javascript checkbox check all

I'm working on a check all button, but I'm a bit too unfamiliar with javascript to do this part. 我正在使用全选按钮,但是我对JavaScript不太熟悉,无法完成此部分。

Essentially, I have a checkbox at the top of the page, and when you check it, it goes through and changes a checkbox in each row of a table to be checked. 本质上,我在页面顶部有一个复选框,当您选中它时,它将遍历并更改要检查的表的每一行中的复选框。

I've looked around at the similar questions and I know I have to use 我环顾了类似的问题,我知道我必须使用

~~~~.prop('checked', ...)

This is the checkbox which I would like to have check all the boxes in the table. 这是我要选中表中所有复选框的复选框。

    <div class="disc-opt" style="float:left">
        Adjust All            
        <div class="make-switch">
            <input type="checkbox" class="create-adjustments"  />
        </div>
    </div>

This is the table row layout. 这是表格行的布局。

<tr class="<?= $data['adjusted'] ? "success" : "" ?>">
    <td>
       Stuff
    </td>
    <td><?= $data['products_name'] ?></td>
    <td><?= $data['total_final_quantity'] ?></td>
    <td><?= $data['total_onhand'] ?></td>
    <td><?= $data['difference'] ?> </td>
    <td><?= $data['difference_cost'] ?></td>
    <!-- The Checkbox to be changed -->
    <td class = "Adjustbox">
        <?php if(!$data['adjusted']): ?>
            <div class="make-switch">
                <input type="checkbox" name="adjust_products[]"
                                    value="<?= $products_id ?>">
            </div>
        <?php else: ?>
            Adjustment Complete
        <?php endif; ?>
    </td>
</tr>

Edit: Here's what I'm currently working with(I'm not sure if this is really poorly set up, it's from a bunch of old code I've never touched before today). 编辑:这是我目前正在使用的内容(我不确定这是否设置得很差,它来自一堆我以前从未接触过的旧代码)。 I've made some edits in the code above as well. 我也在上面的代码中做了一些编辑。

function createAdjustments()
{
    if($("#create-adjustments").is(':checked'))
    {
        //turn all on
    } else {
        //turn all off
    }
}

$('#create-adjustments').change(function(e)
{
    createAdjustments();
});

$('#create-adjustments').trigger('change');

I've been able to access the element in the table row via the document.ready function, but I couldn't get anything to work when I checked the checkbox. 我已经能够通过document.ready函数访问表格行中的元素,但是当我选中该复选框时,我什么都无法工作。

Here's a picture of what I'm trying to do: 这是我要做什么的图片: 左上方的复选框是我要选中的复选框,以检查表中的所有复选框。

I'm trying to make it so that when I check the checkbox on the top left, it goes through the table and checks all of the checkboxes in the right column. 我试图这样做,以便当我选中左上方的复选框时,它会遍历表格并选中右列中的所有复选框。

Take a look at this I did a similar thing earlier and this did the trick for me. 看看这个,我之前做过类似的事情,这为我带来了窍门。 Pretty much, you create a div with all your input boxes and say to mark all of them as checked if the main one is clicked. 几乎,您创建了一个包含所有输入框的div,并说如果单击主输入框,则将所有输入框标记为已选中。

HTML: HTML:

<label><input type="checkbox" name="sample" class="selectall"/> Select all</label>

<div id="checkboxlist">

    <label><input type="checkbox" name="sample[]"/>checkbox1</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox2</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox3</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox4</label><br />

</div>

JAVASCRIPT: JAVASCRIPT:

$('.selectall').click(function() {
    if ($(this).is(':checked')) {
        $('div input').attr('checked', true);
    } else {
        $('div input').attr('checked', false);
    }
});

If you're talking about ~~~~.prop('checked', ...) , then you're talking about jQuery. 如果您正在谈论~~~~.prop('checked', ...) ,那么您正在谈论的是jQuery。

In that case if you want to check a checkbox you would want to provide an ID or class . 在这种情况下,如果要选中一个复选框,则需要提供一个IDclass If you don't want , well : 如果您不想要,那么:

$(".make-switch input:checkbox:first").prop('checked',true)

I ended up solving it using this. 我最终用这个解决了。

if($("#create-adjustments").is(':checked'))
    {
        $("tr").find("td.box").find("div").find("div").prop('class', "switch-on switch-animate");
    } else {
        $("tr").find("td.box").find("div").find("div").prop('class', "switch-off switch-animate");

    }
<label><input type="checkbox" name="sample" class="selectall"/> Select all</label>

<div id="checkboxlist">

    <label><input type="checkbox" name="sample[]"/>checkbox1</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox2</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox3</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox4</label><br />

</div>

$('.selectall').click(function() {     
    if ($(this).is(':checked')) {        
        $('#checkboxlist input[type="checkbox"').prop('checked', true);
    } else {
        $('#checkboxlist input[type="checkbox"').prop('checked', false);
    }
});

demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM