简体   繁体   中英

Why this code does not work in Firefox

I can't understand why this code does not work in Firefox (it works in Safari, Chrome and IE). m1, n1, m2 and n2 are four text input

<button class="btn btn-warning"><a style="color:white;" class="btn" onclick="save()" > Calcola </a></button> ...

... <script type="text/javascript"> 
  function save(){
    var m1 = document.getElementById("m1").value;
    var n1 = document.getElementById("n1").value;

    var m2 = document.getElementById("m2").value;
    var n2 = document.getElementById("n2").value; 

    window.location = ("?clicco=true&m1="+m1+"&n1="+n1+"&m2="+m2+"&n2="+n2);

  }
</script>

将点击事件附加到按钮也可能是一个好主意,因为您没有使用标签上的 href 属性来重定向..

<button class="btn btn-warning" onclick="save()"> Calcola </button>

The reason that it doesn't work is that there are two actions that compete. The default action of the button is to submit the form, so if that is handled before the click event you won't see the effect of the function.

Binding the event on the button instead and returning false from the event handler will stop the default action of the button:

<button class="btn btn-warning" onclick="return save()"><a style="color:white;" class="btn"> Calcola </a></button> ...

... <script type="text/javascript"> 
  function save(){
    var m1 = document.getElementById("m1").value;
    var n1 = document.getElementById("n1").value;

    var m2 = document.getElementById("m2").value;
    var n2 = document.getElementById("n2").value; 

    window.location = ("?clicco=true&m1="+m1+"&n1="+n1+"&m2="+m2+"&n2="+n2);

    return false;
  }
</script>

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