Get the App
SLTechnology News&Howtos  ›  Development  › 

How does JQuery use check or uncheck to control checkbox controls

Shulou Source: shulou.com Published: 2022-06-01 07:34:55 10月01日 Update

This article mainly introduces JQuery how to use check or uncheck to control checkbox control related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this JQuery how to use check or uncheck to control checkbox control article will have a harvest, let's take a look.

If we want to use JQuery to check or checkbox controls on uncheck pages, we might want to use the following code:

$('# chk-all'). On ('click', function () {

Var checked = $(this). Is (': checked')

$("input [type = 'checkbox'] [name='chk-att']"). Attr ('checked', checked)

})

Chk-all is a checkbox control, and we want to select all or deselect all by clicking on it. In the onclick event, first pass $(this). Is (': checked') to get the current checked state of the checkbox, and then check or uncheck the checkbox controls for all name='chk-att' on the page. The method used here is to change the value of the checked attribute, but this method does not always work. Sometimes you will see that the attribute value of the element has been changed, but the selected state has not changed. Try to change the attr () method to removeAttr () to uncheck it, but it still doesn't work!

The solution is to use the prop () function instead of the attr () function in the above code. Modify the above code as follows:

$('# chk-all'). On ('click', function () {

Var checked = $(this). Is (': checked')

$("input [type = 'checkbox'] [name='chk-att']"). Prop ('checked', checked)

})

This is the end of the article on "how JQuery uses check or uncheck to control checkbox controls". Thank you for reading! I believe you all have a certain understanding of "how JQuery uses check or uncheck to control checkbox controls". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Controls controls code methods knowledge content functions properties states articles facets pages events values functions elements methods functions simple operations articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi OPPO Reno NVidia Shulou Tech Info