سؤال

i have 2 inputfields

<input id="pass1" name="pass1" value="" size="12" type="password" />
<input id="pass2" name="pass2" value="" size="12" type="password" />

when id="pass1" has no value (is empty) and id="pass2" has a value i want to clear id="pass2" with jquery before i submit the form.

is that possible?

هل كانت مفيدة؟

المحلول

i think checking the second input is useless and has no logical reason, i mean in both cases you end up with empty #pass2

$("form").on('submit',function(){
      if($("#pass1").val() == ''){
          $("#pass2").val('');
          return false;
      }
});

نصائح أخرى

See you have to do a form submit and check if the #pass1 is empty or not and return false or event.preventDefault() to stop the form to submit.

$(function(){
  $("yourFormId").submit(function(e){
    if($("#pass1").val() == ''){
       $("#pass2").val('');
       e.preventDefault(); //<----stop the form submission here after clearing
    }
  });
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top