Javascript jump to anchor based on submit value/name

时光毁灭记忆、已成空白 提交于 2019-12-23 04:49:16

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/12341909/javascript-jump-to-anchor-based-on-submit-value-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!