0

Не могу понять, почему при клике на картинку отправляется форма, ведь у меня прописано, что форму отправлять только при изменении. Помогите, люди добрые, поставить на путь истинный :)

https://jsfiddle.net/hibinyru/jzt5c9mn/11/

$(".iframe-button-add").bind("click", function(){
  $("#upload").click();
});

$("#upload").change(function(){
  this.form.submit();
  $(".content_more").slideToggle(300);
});
.iframe-button-add {
  width:80px;
  height:80px;
  border-radius: 3px;
  border: solid 1px #ebebeb;
  background: no-repeat center url('https://www.hibiny.com/images/2020/interface/buttons/icons-options-photo-add.png');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<form action="" method="post" enctype="multipart/form-data">
  <div class="content_more" style="display:none;"><img src="/images/2020/interface/preloader/64.gif"></div>

  <button class="iframe-button-add" ></button>
  <input type="file" name="files" id="upload" accept="image/jpeg,image/png,image/gif,image/jpg,image/bmp,image/webp" style="display:none;">

  <input type="hidden" name="form" value="1">
</form>

1 ответ 1

1

Элемент button без атрибута type отправляет форму по умолчанию. Чтобы это запретить, сделайте event.preventDefault() :

$(".iframe-button-add").bind( "click", function(e){
    e.preventDefault();
    $("#upload").click();
});

или напишите кнопке атрибут type="button"

1
  • блин, как все просто оказалось... спасибо!! :)
    – hibinyru
    19 янв 2021 в 11:16

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.