Change in checkbox is not working

Asked

Viewed 351 times

0

Good afternoon.

Problem solved: I really thought it was strange that it worked out here and I came to the conclusion that it might be the fact that the checkbox(s) dynamically generated. And that’s what it was! The solution was to put change inside the Ajax Success function, right after the checkbox(s) were printed on the screen. It looked something like this:

function getLotes(){
    var options = {
        url: "formhelper.php",
        type: "POST",
        data: {acao: 'getLotes'},
        success: function (data) {
            $('.lote-checkbox').empty().html(data));

            $('input[id^="lotebox"]').on('change', function(){
               alert('checou ou não checou');
            });
        }
    };
    $('form[name="rlManejo"]').ajaxForm(options).submit();
}

lotebox was the last of the attempts I made, trying to make it work.

The command $('form[name="rlManejo"]'). ajaxForm(options). Submit(); is part of the malsup library (jQuery Form Plugin: http://jquery.malsup.com/form/#ajaxForm) we are using for this application (if anyone wants to know/understand).

Thank you all so much for trying to help. In the end that was the problem.

Big hug!

Primary problem:

When I mark/uncheck the checkbox, the alert (used only for testing) does not appear.

The label changes color as programmed in the CSS, but the checkbox is not checked (checked via Google Chrome/Elements). No error message on console either.

It looks the same to me as @Victor Carnaval posted in response and it works here on Stack, but not on my website. It’s the only element that’s giving trouble.

jquery/javascript code:

$('form[name="rlManejo"] input[name="lote[]"]').on('change', function(){
alert('checou ou não checou');
});

HTML:

<form name="rlManejo"><!-- incorporado em edição aqui, mas presente no código original -->
    <div class="select-checkbox">
            <button type="button" class="select-checkbox-btn">Lotes</button>
            <div class="select-checkbox-content lote-checkbox">
                <input type="checkbox" name="lote[]" id="lote-checkbox0" value="1">
                <label for="lote-checkbox0">Lote 1</label>
                <input type="checkbox" name="lote[]" id="lote-checkbox1" value="2">
                <label for="lote-checkbox1">Lote 2</label>
            </div>
        </div>
    </form><!-- incorporado em edição aqui, mas presente no código original -->

CSS:

.select-checkbox-btn {
    padding: 7px !important;
    border: 1px solid #DDD !important;
    color: #FFF !important;
    background-color: #003eff !important; /* #FF6347 */
    background-image: linear-gradient(to bottom, transparent, rgba(0,0,0,.21)) !important;
    cursor: pointer !important;
    border-radius: 10px !important;
    -moz-border-radius: 10px !important;
    -webkit-border-radius: 10px !important;
}

.select-checkbox {
    width: auto !important;
    height: auto !important;
    margin: 10px 8px 0 0 !important;
    position: relative !important;
    display: inline-block !important;
}

.select-checkbox-content {
    display: none !important;
    position: absolute !important;
    width: auto !important;
    height: auto !important;
    background-color: #f9f9f9 !important;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2) !important;
    z-index: 1 !important;
}

.select-checkbox-content input[type=checkbox]{ display: none !important; }
.select-checkbox-content input[type=checkbox] + label{
    color: black !important;
    width: auto !important;
    padding: 5px 3px !important;
    border: #000 1px solid !important;
    text-decoration: none !important;
    display: block !important;
    cursor: pointer !important;
}
.select-checkbox-content input[type=checkbox]:checked + label{ 
    background-color: #2F4F4F !important; 
    color: white !important; 
}
.select-checkbox-content input:hover {background-color: #f1f1f1 !important; }
.select-checkbox:hover .select-checkbox-content{ display: block !important; }
.select-checkbox:hover .select-checkbox-btn{ display: block !important; }
form[name="getDespesas"] .select-checkbox-content, form[name=D2R] .select-checkbox-content{ width: 200px !important; }
  • How it works if you do the onChange() in that element 'form[name="rlManejo"] that doesn’t exist?

  • Good afternoon, @Leandrade. In my code the div is inside the form with this name.

  • Okay, so, a tip. Every time you ask a question, put as much information as possible with all parts of the code in question.

1 answer

1

In your Jquery selector the element is filtered form with the attribute name=rlManejo. Just add the tag form before the checkbox element.

$(function() {
  $('form[name="rlManejo"] input[name="lote[]"]').on('change', function() {
    alert('checou ou não checou');
  });
});
  .select-checkbox-btn {
  padding: 7px !important;
  border: 1px solid #DDD !important;
  color: #FFF !important;
  background-color: #003eff !important;
  /* #FF6347 */
  background-image: linear-gradient(to bottom, transparent, rgba(0, 0, 0, .21)) !important;
  cursor: pointer !important;
  border-radius: 10px !important;
  -moz-border-radius: 10px !important;
  -webkit-border-radius: 10px !important;
}

.select-checkbox {
  width: auto !important;
  height: auto !important;
  margin: 10px 8px 0 0 !important;
  position: relative !important;
  display: inline-block !important;
}

.select-checkbox-content {
  display: none !important;
  position: absolute !important;
  width: auto !important;
  height: auto !important;
  background-color: #f9f9f9 !important;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2) !important;
  z-index: 1 !important;
}

.select-checkbox-content input[type=checkbox] {
  display: none !important;
}

.select-checkbox-content input[type=checkbox]+label {
  color: black !important;
  width: auto !important;
  padding: 5px 3px !important;
  border: #000 1px solid !important;
  text-decoration: none !important;
  display: block !important;
  cursor: pointer !important;
}

.select-checkbox-content input[type=checkbox]:checked+label {
  background-color: #2F4F4F !important;
  color: white !important;
}

.select-checkbox-content input:hover {
  background-color: #f1f1f1 !important;
}

.select-checkbox:hover .select-checkbox-content {
  display: block !important;
}

.select-checkbox:hover .select-checkbox-btn {
  display: block !important;
}

form[name="getDespesas"] .select-checkbox-content,
form[name=D2R] .select-checkbox-content {
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

<form name="rlManejo">
  <div class="select-checkbox">
    <button type="button" class="select-checkbox-btn">Lotes</button>
    <div class="select-checkbox-content lote-checkbox">
      <input type="checkbox" name="lote[]" id="lote-checkbox0" value="1">
      <label for="lote-checkbox0">Lote 1</label>
      <input type="checkbox" name="lote[]" id="lote-checkbox1" value="2">
      <label for="lote-checkbox1">Lote 2</label>
    </div>
  </div>
</form>

  • Good afternoon, @Victor Carnaval. I have this element, but with or without it the command does not work. I’m taking a ball! hehehe

  • @E.Rabbit, I updated my answer just by adding the form tag and see that it’s working.

  • Puts a class in your inputs or instead of searching for the name="lot[]". That way you made it invalid, as if you had 2 objects with the same id.

  • In fact, I don’t understand why it’s working here on Stack and my page isn’t working. I’ve tried, for example: $('. batch-checkbox input'). on('change', Function(){... and does not work.

  • Make sure that Jquery is running right after the DOM is ready for manipulation; see if there is another element with the same attributes and also if there is no other function or event that is overwriting that function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.