mercredi 22 mars 2017

Changing values of inputs via checkbox change event

I have a page which has two check boxes with different id's along with a text box in different div and radio group consisting of 4 radio buttons in another div. What I'm trying to achieve is:

If user checks the first checkbox the following should happen

  1. checkbox two to be disabled
  2. textbox to have a value of 0 and made readonly
  3. first radio selected and rest hidden along with their labels

If user unchecks the first checkbox the the above steps should be revereted.

If user checks the second checkbox the following should happen

  1. checkbox one to be disabled
  2. textbox to have value of 1 abd made readonly
  3. first radio selected and rest hidden along with their labels

If user unchecks the second checkbox the the above steps should be revereted.

I have come up with the following code

$("#firstCheckBox").on("change", function () {
    if ($(this).is(":checked")) {
        $('#secondCheckBox').prop('disabled', true);
        $('#spinner').val('0');
        $('#spinner').prop('readonly', true);
        $('#radio_id:first').prop('checked', true);
        $('#radio_id:not(:checked)').hide();

    } else {
        $('#secondCheckBox').prop('disabled', false);
        $('#spinner').val('');
        $('#spinner').prop('readonly', false);
        $('#radio_id:first').prop('checked', false);
        $('#radio_id:not(:checked)').show();
    }
});
$("#secondCheckBox").on("change", function () {
    if ($(this).is(":checked")) {
        $('#firstCheckBox').prop('disabled', true);
        $('#spinner').val('1');
        $('#spinner').prop('readonly', true);
        $('#radio_id:first').prop('checked', true);
        $('#radio_id:not(:checked)').hide();

    } else {
        $('#firstCheckBox').prop('disabled', false);
        $('#spinner').val('');
        $('#spinner').prop('readonly', false);
        $('#radio_id:first').prop('checked', false);
        $('#radio_id:not(:checked)').show();
    }
});

So my question, is there a better way of doing this or am I on the right track?




Aucun commentaire:

Enregistrer un commentaire