A
A
Anatoly Teorsky2020-02-18 05:59:46
JavaScript
Anatoly Teorsky, 2020-02-18 05:59:46

Why is checkbox value reset on change event and ajax submit?

Good day.
I implement sending ajax request when clicking on the checkbox

<div id="div-pay">
    <div class="deliveri-block">
      <div id="deliveri-list">
      <? foreach ($arResult['DELIVERY_LIST'] as $arDelivery):?>
          <label>
              <input type="radio" name="delivery_id" class="deliveri-el"
                     value="<?= $arDelivery['ID'] ?>">
              <?= $arDelivery['NAME'] ?>,
              <?= $arDelivery['PRICE_DISPLAY'] ?>
          </label>
          <br>
      <? endforeach; ?>
      </div>
    </div>
   
   <div class="payment-block">
      <div id="payment-list">
      <? foreach ($arResult['PAY_SYSTEM_LIST'] as $arPaySystem): ?>
          <label>
              <input type="radio" name="pay_system_id"
                     value="<?= $arPaySystem['ID'] ?>"
                  <?= $arPaySystem['CHECKED'] ? 'checked' : '' ?>>
              <?= $arPaySystem['NAME'] ?>
          </label>
          <br>
      <? endforeach; ?>
    </div>
  </div>
</div>


checkboxes = Array.from(document.querySelectorAll('.deliveri-el'));
checkboxes.forEach(function(checkbox, i) {

    checkbox.onchange = function() {

        $.ajax({
            url: BX.message('TEMPLATE_PATH') + "/ajax.php",
            type: 'POST',
            data: {
                settings: this.name,
                id: this.value,
                checked: this.checked ? 1:0,
                delivery_id: $(this).val()
            },
            beforeSend: function() { checkbox.disabled = true; },
            complete: function() { checkbox.disabled = false; },
            success: function(response) {
               $('#payment-list').html(response);
            }
        });
    }
});

I tried in many ways, but in all cases, after ajax renders the response, the checkbox ceases to be checked.
It looks something like
5e4b52c0df23a847462900.png
this after I clicked on one of the deliveries..
Please tell me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Teorskiy, 2020-02-18
@mamkaror

UPD. In general, the error disappeared as soon as I changed the name attribute to another one. Although the previous ANYWHERE is no longer involved. what was the mistake. Thanks for the advice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question