If Load javascript

Asked

Viewed 110 times

0

I have a following script. It’s two checkboxes that will work like two radio Buttons, which happens to me, when I click on a checkbox it disables all the questions in the form and also disables the checkbox that was not selected, and when I save my form and go back to it, the checkbox I selected is clicked normal, plus all questions in my form are not disabled. Could you help me?

$(document).ready(function() {
  Eventos();

  $('[name="dtd1nrob"]').change(function() {
    verificaCamposExibirOcultar4();
  });

  $('[name="dtd1nrob"]').change(function() {
    verificaCamposExibirOcultar5();
  });
});

function Eventos() {
  verificaCamposExibirOcultar4();
  verificaCamposExibirOcultar5();
};

function verificaCamposExibirOcultar4() {
  var value = $('[name="dtd1nrob"]:checked').val();
  if (value == '1') {
    $('#dtd1nrob2').prop('disabled', true).attr('disabled');
  } else {
    $('#dtd1nrob2').prop('disabled', false).attr('disabled');
  }
}

function verificaCamposExibirOcultar5() {
  var value = $('[name="dtd1nrob"]:checked').val();
  if (value == '2') {
    $('#dtd1nrob1').prop('disabled', true).attr('disabled');
  } else {
    $('#dtd1nrob1').prop('disabled', false).attr('disabled');
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
  <div class="form-group">
    <div class="col-lg-12">
      <div class="col-lg-12">
        <div class="checkbox">
          <label>
            <input type="checkbox" id="dtd1nrob1" checked="@(Model.dtd1nrob == "1" ? true : false)" name="dtd1nrob" value="1">Não realizada por óbito em data anterior</label>
        </div>
      </div>
      <div class="col-lg-12">
        <div class="checkbox">
          <label>
            <input type="checkbox" id="dtd1nrob2" checked="@(Model.dtd1nrob == "2" ? true : false)" name="dtd1nrob" value="2">Não realizada por outro motivo</label>
        </div>
      </div>
    </div>
  </div>
</div>

  • Where are your form questions?

  • What do you need help with? When selecting checkbox should not disable the entire form? What should happen when you "return" to the form? And more importantly, if the checkboxes will work like radios, why don’t you use the radios? Detail: you are not properly escaping the characters in the "checked" inputs events.

  • like I’m "back" to your form?

  • 1

    Just one note: you have two checkboxes with the same name, but you don’t have to declare the change event twice. He’ll know what it’s about in a phone call.

  • Then I cannot use radio button, because it will get stuck in this question, these two checkbox disable all questions of the form, if none of them is selected the user can fill out the form normally. Do you understand ? @Andersoncarloswoss

  • How is it that neither of the two will be able to be marked if they should act as radio Buttons?

  • The problem is at load time, when I load the page I wanted all options to be disabled

  • Qndo tu diz: all options, what are these options? Divs with textarea, checkbox... ?

  • @Aline Follow the example I put Run the script and see please!

Show 4 more comments

1 answer

0


$(document).ready(function () {
    verificarCheckbox();
    $('[name="dtd1nrob"]').change(function () {
        verificarCheckbox();
    });
});

function verificarCheckbox() {
    $(".todosOsOutrosElementos").show();
    $('[name="dtd1nrob"]').prop('disabled', false).attr('disabled')

    if ($("#dtd1nrob1").is(":checked"))
        $('#dtd1nrob2').prop('disabled', true).attr('disabled');
    else if ($("#dtd1nrob2").is(":checked"))
        $('#dtd1nrob1').prop('disabled', true).attr('disabled');

    if ($('[name="dtd1nrob"]').is(":checked"))
        $(".todosOsOutrosElementos").hide();
}

Replace: $(".all Elements"). show()/Hide() by its elements.

Browser other questions tagged

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