@LastGeneral

Как при выборе input type="radio" активировать input type="checkbox"?

Он добавляет флажок, но не убирает его, что не так сделал?
<ul id="shipping_method" class="woocommerce-shipping-methods">
  <li>
    <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_local_pickup18" value="local_pickup:18" class="shipping_method" checked="checked"><label for="shipping_method_0_local_pickup18">Самовывоз</label>
  </li>
  <li>
    <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_nova_poshta_shipping19" value="nova_poshta_shipping:19" class="shipping_method"><label for="shipping_method_0_nova_poshta_shipping19"><span id="wcus-shipping-cost">Новая почта</span></label><input id="wcus-shipping-name" type="hidden" value="Новая почта">					
  </li>
  <li>
    <input type="radio" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate25" value="flat_rate:25" class="shipping_method"><label for="shipping_method_0_flat_rate25">Курьером</label>					
  </li>
</ul>

<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
				<input id="ship-to-different-address-checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" type="checkbox" name="ship_to_different_address" value="1"> <span>Доставить на другой адрес?</span>
</label>

ul li {
      list-style-type: none;
}

$(function () {
    $("#shipping_method_0_flat_rate25").on("change", function () {
        if ($("#shipping_method_0_flat_rate25").prop("checked")) {
            $("#ship-to-different-address-checkbox").attr("checked", true);
        } else {
            $("#ship-to-different-address-checkbox").attr("checked", false);
        }
    });
});
  • Вопрос задан
  • 123 просмотра
Решения вопроса 1
MaKvc
@MaKvc
Отчаянный веб-разработчик
$(function () {
    $("input[type='radio']").on("change", function (event) {
        if (event.target.id == "shipping_method_0_flat_rate25") {
            $("#ship-to-different-address-checkbox").attr("checked", true);
        } else {
            $("#ship-to-different-address-checkbox").removeAttr("checked");
        }
    });
});

Так?
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
KickeRocK
@KickeRocK
FrontFinish
$(function () {
    $("#shipping_method_0_flat_rate25").on("change", function () {
        if ($("#shipping_method_0_flat_rate25").prop("checked")) {
            $("#ship-to-different-address-checkbox").attr("checked", true);
        } else {
            $("#ship-to-different-address-checkbox").removeAttr("checked");
        }
    });
});

checked - Boolean; if present, the checkbox is tog...
То есть даже если у него значение "false" - он будет включен, чтобы отключить нужно его удалить.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы