简体   繁体   中英

Joomla Site - have a form that calls a javascript function - but it also is redirecting my url - why?

the code:

javascript:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

<script type="text/javascript">
function scroll(pos)
{
    var x = location.hash;
     var scrolamount = pos;
    console.log(scrolamount);
    $('html, body').animate(
    {
        scrollTop: scrolamount
    }, 400);
    var d = document.createElement("div");
    d.style.height = "101%";
    d.style.overflow = "hidden";
    document.body.appendChild(d);
    setTimeout(function ()
    {
        d.parentNode.removeChild(d);
    }, 100);
    return false;
}
</script>

html

this function shows the alert and then returns to page as normal

<div id="tester">
<form action="javascript:alert('action sent');" >
<input type="submit" value="But1">
 </form>

this form scrolls the page as expected but then immediately redirects the URL to the main page

<div id="but1">
 <form  onsubmit="scroll(400)" >
<input type="submit" value="But1">
          </form>       
</div>

You are not preventing the form to submit. Try adding this :

onsubmit="scroll(400); return false;"

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