Javascript jump to anchor based on submit value/na

2019-06-11 04:26发布

I am working on a page that is basically a long form, with tables of info followed by a submit button after each table.

The form submits to itself and changes the info in the tables based on user input.

What I would like to do is have the page jump to "Section X" if "Submit Button X" is pressed, jump to "Section Y" if "Submit Button Y" is pressed, etc.

I'm currently using this Javascript in the head of the doc to try and accomplish this:

<script type="text/javascript">

var anchors = new Array(

<?php 
for($i = 0; $i < 13; $i++) {
    $anchors =  "\"$divTag[$i]\"";
    if($i < 12) {
        $anchors .= ", ";
    }
    echo $anchors;
} ?>

)

function jumpToAnchor() {

    <?php if(isset($_POST["submit_fjw"])) { ?>

    window.location = String(window.location).replace(/\#.*$/, "") +
    "#" + anchors[0];

    <?php } if(isset($_POST["submit_jwl"])) { ?>

    window.location = String(window.location).replace(/\#.*$/, "") +
    "#" + anchors[1];

    <?php } ?>
}

It works... kinda. It doesn't jump on the first press of the Submit button, but the second.

I'm open to suggestions... less javascript is better, because my javascript skills are weak at best. Could I use a variable tied to the submit button that will pass a value to the PHP script and correctly jump to the right section?

2条回答
疯言疯语
2楼-- · 2019-06-11 04:46

May be you find more convenient to use http://www.w3schools.com/html/html_links.asp (section HTML Links - The name Attribute) instead javascript? Just a suggestion.

查看更多
祖国的老花朵
3楼-- · 2019-06-11 04:52

Have you tried something like this?

<form action="thisfile.php#section2" name="form1" ... >
....
</form>

<a name="section2">
<form action="thisfile.php#section3" name="form2" ... >
....
</form>

<a name="section3">
<form ....

when form1 is submitted, file reloads and automaticly goes to anchor named section2, when form2 is submitted, it scrolls to section3, etc

查看更多
登录 后发表回答