A
A
anton_kP2021-12-06 13:25:30
JavaScript
anton_kP, 2021-12-06 13:25:30

Is event.preventDefault() blocking input?

I already asked a similar question , but I did not understand the problem.
I noticed that the text is not entered into the form in the input on ios 13 and below in Safari, in other browsers on different devices everything is fine.
It's probably in the script, the window with the form opens modally, has several forms.
Maybe there is an error in it when the standard behavior of the browser is canceled? Event.preventDefault() is used several times, but in clicks on the submit...

jQuery(document).ready(function($){
  var formModal = $('.cd-user-modal'),
    formLogin = formModal.find('#cd-login'),
    formSignup = formModal.find('#cd-signup'),
    formReset = formModal.find('#cd-reset-password'),
    formForgotPassword = formModal.find('#cd-reset-password'),
    formModalTab = $('.cd-switcher'),
    tabLogin = formModalTab.children('li').eq(0).children('a'),
    tabSignup = formModalTab.children('li').eq(1).children('a'),
    forgotPasswordLink = formLogin.find('.cd-form-bottom-message a'),
    backToLoginLink = formForgotPassword.find('.cd-form-bottom-message a'),
    mainNav = $('.main-nav1');

  //open modal
  mainNav.on('click', function(event){
    $(event.target).is(mainNav) && mainNav.children('ul').toggleClass('is-visible');
  });

  //open sign-up form
  mainNav.on('click', '.cd-signup', signup_selected);
  //open login-form form
  mainNav.on('click', '.cd-signin', login_selected);

  //close modal
  formModal.on('click', function(event){
    if( $(event.target).is(formModal) || $(event.target).is('.cd-close-form') ) {
      formModal.removeClass('is-visible');
    }	
  });
  //close modal when clicking the esc keyboard button
  $(document).keyup(function(event){
    	if(event.which=='27'){
    		formModal.removeClass('is-visible');
      }
    });

  //switch from a tab to another
  formModalTab.on('click', function(event) {
    event.preventDefault();
    ( $(event.target).is( tabLogin ) ) ? login_selected() : signup_selected();
  });

  //hide or show password
  $('.hide-password').on('click', function(){
    var togglePass= $(this),
      passwordField = togglePass.prev('input');
    
    ( 'password' == passwordField.attr('type') ) ? passwordField.attr('type', 'text') : passwordField.attr('type', 'password');
    ( 'Show' == togglePass.text() ) ? togglePass.text('Show') : togglePass.text('Hide');
    //focus and move cursor to the end of input field
    passwordField.putCursorAtEnd();
  });

  //show forgot-password form 
  forgotPasswordLink.on('click', function(event){
    event.preventDefault();
    forgot_password_selected();
  });

  //back to login from the forgot-password form
  backToLoginLink.on('click', function(event){
    event.preventDefault();
    login_selected();
  });

  function login_selected(){
    mainNav.children('ul').removeClass('is-visible');
    formModal.addClass('is-visible');
    formLogin.addClass('is-selected');
    formSignup.removeClass('is-selected');
    formForgotPassword.removeClass('is-selected');
    tabLogin.addClass('selected');
    tabSignup.removeClass('selected');
  }

  function signup_selected(){
    mainNav.children('ul').removeClass('is-visible');
    formModal.addClass('is-visible');
    formLogin.removeClass('is-selected');
    formSignup.addClass('is-selected');
    formForgotPassword.removeClass('is-selected');
    tabLogin.removeClass('selected');
    tabSignup.addClass('selected');
  }

  function forgot_password_selected(){
    formLogin.removeClass('is-selected');
    formSignup.removeClass('is-selected');
    formForgotPassword.addClass('is-selected');
  }


$("#senderauth").on("submit", function(e){
e.preventDefault();
  $.ajax({
    url: '/profile/api.php',
    method: 'post',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(data){
      $('#res_main_mail2').html(data);
    }
  });
});
$("#senderregistr").on("submit", function(e){
e.preventDefault();
  $.ajax({
    url: '/profile/registr.php',
    method: 'post',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(data){
      $('#res_main_mail5').html(data);
    }
  });
});

$("#senderedit").on("submit", function(e){
e.preventDefault();
  $.ajax({
    url: '/profile/edit.php',
    method: 'post',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(data){
      $('#res_main_mail6').html(data);
    }
  });
});

$("#sendereditbonus").on("submit", function(e){
e.preventDefault();
  $.ajax({
    url: '/profile/bonusedit.php',
    method: 'post',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(data){
      $('#res_main_mail7').html(data);
    }
  });
});

var btn = document.getElementById("sendresets");

$("#senderreser").on("submit", function(e){
    btn.disabled = true;

document.getElementById('sendresets').style.display= 'none';
document.getElementById('fixresets').style.display= 'block';
document.getElementById('sendresets2').style.display= 'block';


setTimeout(function() {
    login_selected();
}, 700000);


    window.setTimeout(function() {
document.getElementById('sendresets').style.backgroundColor = '#141e36';

       btn.disabled = false; 
    }, 60000); 
e.preventDefault();
  $.ajax({
    url: '/profile/apireset.php',
    method: 'post',
    dataType: 'html',
    data: $(this).serialize(),
    success: function(data){
      $('#res_main_mail3').html(data);
    }
  });
});
  

});


//credits http://css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
jQuery.fn.putCursorAtEnd = function() {
  return this.each(function() {
    	// If this function exists...
    	if (this.setSelectionRange) {
      		// ... then use it (Doesn't work in IE)
      		// Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
      		var len = $(this).val().length * 2;
      		this.focus();
      		this.setSelectionRange(len, len);
    	} else {
    		// ... otherwise replace the contents with itself
    		// (Doesn't work in Google Chrome)
      		$(this).val($(this).val());
    	}
  });
};

is there a bug in the code and is it possible to pass to explicitly state that the behavior of event.preventDefault() should only be on the submit button in submit forms?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question