The abuse of antibiotics has led to the enhancement of bacterial drug resistance, which has aroused widespread concern. In our study, an unusual polymer (PADAAC) with aldehyde, vinyl and azo groups was obtained by copolymerization of allyl diazoacetate (ADA) and acrolein (AC). We fabricated a series of hydrogels (PA0AM, PA5AM, PA10AM and PA20AM) by incorporation PADAAC into the acrylamide and N, N′-methylene bis(acrylamide) networks, which can suppress bacterial growth and kill the bacteria with > 95%. All the hydrogels possessed low cytotoxicity. More remarkably, the aldehyde groups were protected amino groups via a simple imine reaction. Protected PA20AM had no effect on the bacteria, while deprotected PA20AM recovered its antimicrobial property against Staphylococcus aureus (killing efficiency: 96.1%), Escherichia coli (killing efficiency: 98.3%) and methicillin-resistant Staphylococcus aureus (MRSA) (killing efficiency: 95.0%) after removing the amino groups. PA20AM and deprotected PA20AM also exhibited good antimicrobial property in vivo.