JavaScript根据提交值/名称跳转到锚点 [英] Javascript jump to anchor based on submit value/name

查看:122
本文介绍了JavaScript根据提交值/名称跳转到锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



表单提交给自己,然后在提交表单后,根据用户输入更改表格中的信息。



如果按下Submit Button X按钮,页面跳转到Section X ,如果按下Submit Button Y,则跳转到Section Y等。



我目前在文档的头部使用这个Javascript来尝试和完成这:

 < script type =text / javascript> 

var anchors = new Array(

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



函数jumpToAnchor(){

<?php if (isset($ _ POST [submit_fjw])){?>
$ b $ window.location = String(window.location).replace(/ \#。* $ /,)+
#+ anchors [0];

<?php} if(isset($ _ POST [submit_jwl])){?>
$ b window.location = String(window.location).replace(/ \#。* $ /,)+
#+ anchors [1];

<?php}?>
}

它有效...有点。它不会在第一次按提交按钮时跳转,而是第二次按下。

我对建议持开放态度...... JavaScript越少越好,因为我的JavaScript技能最好是弱的。我可以使用一个绑定到提交按钮的变量,它将一个值传递给PHP脚本并正确跳转到正确的部分吗?

解决方案

<

 < form action =thisfile.php#section2name =form1 ...> 
....
< / form>

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

< a name =section3>

当form1被提交时,文件重新加载并自动运行定位命名的section2,当form2被提交时,它会滚动到section3等。


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?

解决方案

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

这篇关于JavaScript根据提交值/名称跳转到锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆