Home  >  Article  >  Web Front-end  >  jquery operates checkbox to select all and deselect all_jquery

jquery operates checkbox to select all and deselect all_jquery

WBOY
WBOYOriginal
2016-05-16 16:50:301015browse

Today I wrote a function to select all and cancel all:

Select all: $(":checkbox").attr("checked","checked");

Cancel all Select: $(":checkbox").removeAttr("checked");

Get selected: $(":checked[checked='checked']")

Discover all points You can select it. You can also click to cancel the selection of all. The selected one will be invalidated. Clicking any button next time will have no effect.

This doesn’t work. I don’t know why. This is a problem with scripting languages. It was flexible and weird at the same time, and I didn’t look for the reason. Later I changed it to this:

Select all: $(":checkbox").prop("checked",true)

Cancel all Select: $(":checkbox").prop("checked",false)

Get checked: $(":checkbox:checked")

Invert selection:

Copy code The code is as follows:

$(":checkbox").each(function(){
if($(this).prop("checked")){
$(this).prop("checked",false);
}else{
$(this).prop("checked ",true);
}
});
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn