表格跳转到页面

时间:2010-10-08 21:57:29

标签: javascript html

如何创建一个允许用户输入数字的表单,然后转到example.com/page/#,其中#是用户键入的内容?我希望这是一个分页“跳转”选项。

我只能使用HTML和JavaScript

2 个答案:

答案 0 :(得分:0)

<script type="text/javascript">
    function goToPage(){
        var page=document.getElementById('in').value; // value of input box
        page=page*1; // ensures page is numeric only
        location.href='http://www.example.com/page/'+page; // redirect to new url
    }
</script>
<input type="text" id="in"/>
<input type="button" onclick="goToPage();"/>

答案 1 :(得分:0)

<form id="jump" method="get" action="">
    <input type="text" id="jump-page"/>
    <input type="submit" value="Go"/>
</form>

<script type="text/javascript">
    document.getElementById('jump').onsubmit= function() {
        location.hash= document.getElementById('jump-page').value;
        return false;
    };
</script>

对于页码,您应该在id="page-2"之前添加前缀(以生成有效的标识符),然后执行location.hash= '#page-'+...