简体   繁体   中英

Combining checking box to activate text field and limited number of allowed checked boxes

I've combined two scripts on my page. The first one ( http://htmlgoodies.com/tutorials/forms/article.php/3479181 ) makes sure that the user can only check 2 checkboxes in total. The second one forces the user to check a checkbox to activate a text field or other source of input.

$('#surname').change(function(){
   $("#surname_input").prop("disabled", !$(this).is(':checked'));
});

This is working fine except for one thing. If you check three checkboxes and get the error message hence "unchecking" the checkbox, the input field connected to the checkbox is still activated as if the checkbox was actually checked. Is there any way to make sure that the script only activates the input field if the checkbox is actually checked and not just clicked?

You can try it out yourselves here:

http://leohård.se/avc/form.php

Thank you in advance!

first your code is actually wont work because your newcount is a local variable so everytime you call this function it becomes 0. In order to make this work just put the variable newcount outside the function keepCount and try this code

if (document.resume.title.checked)
{
    NewCount = NewCount + 1;

    if (NewCount == 3)
     {
            //uncheck your checkbox here
        NewCount = NewCount - 1; //decrement the newcount by 1 so now the count is back to 2
     }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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