简体   繁体   中英

event prevent default not working in firefox

This is a kind of similar duplicate to some others here, but I think I'm using event.preventDefault() correctly in this case.

this my html :

 <input type="submit" id="submit" value="Setuju dan kirim" onclick="check_empty_2()">

in Chrome, it is work. but in Firefox, it does not work.

this my code :

function check_empty_2() {

if (document.getElementById('reg_nama').value == "" || document.getElementById('reg_email').value == "" || document.getElementById('reg_no').value == "" || document.getElementById('reg_rumah').value == "" ) {
    alert("Harap isi semua bidang !");
} else {
    document.getElementById('reg_rfid').style.display = "none";
    document.getElementById('alert_rfid').style.display = "block";
    $('html, body').animate({
        scrollTop: $("#alert").offset().top - 150
    });
    event.preventDefault();
}

}

please correct my code, Thank you

you need to read and pass event as argument like

<input onclick="check_empty_2(event)" ...

and

function check_empty_2(event) {
...

For firefox to work change it to

 <input type="submit" id="submit" value="Setuju dan kirim" onclick="check_empty_2(event)">

And the function to

function check_empty_2(event) {....

You Forgot to pass Event,From Current Scenario You will Get error Event is undefine in Firebug

 <input type="submit" id="submit" value="Setuju dan kirim" onclick="check_empty_2(event)">

function check_empty_2(event) {
...}

First a fall. You have written code wrong. event.preventDefault(); should not inside of else. It should be:

function check_empty_2(event) {

if (document.getElementById('reg_nama').value == "" ||     document.getElementById('reg_email').value == "" || document.getElementById('reg_no').value == "" || document.getElementById('reg_rumah').value == "" ) {
 alert("Harap isi semua bidang !");
} else {
document.getElementById('reg_rfid').style.display = "none";
document.getElementById('alert_rfid').style.display = "block";
$('html, body').animate({
    scrollTop: $("#alert").offset().top - 150
});

}
event.preventDefault();
}

On more way to do:

<input type="submit" id="submit" value="Setuju dan kirim">

$(function(){
  $('#submit').click(function(event){ 
  if (document.getElementById('reg_nama').value == "" || document.getElementById('reg_email').value == "" || document.getElementById('reg_no').value == "" || document.getElementById('reg_rumah').value == "" ) {
   alert("Harap isi semua bidang !");
  } else {
    document.getElementById('reg_rfid').style.display = "none";
    document.getElementById('alert_rfid').style.display = "block";
    $('html, body').animate({
        scrollTop: $("#alert").offset().top - 150
    });
  }
  event.preventDefault();
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM