dtotuki47568 2016-01-25 19:34
浏览 52
已采纳

如何根据选择输入使用javascript更改页面内容?

Let's say I have a webpage like this:

    <div style="margin:auto;text-align:center;padding-top:10px;">
    <form action="" method="POST">
        <p style="text-align:center;">
        <select>
            <option value="blogs">blogs.php</option>
            <option value="portfolio">portfolio.php</option>
            <option value="contact">contact.php</option>
            <option value="home">home.php</option>
        </select>
        </p>
        <p style="text-align:center;">
            <input type="submit" name="submit" value="Submit"/>
        </p>
    </form>
</div>

<div class="pagetitle">
    <h5>Option Value (for example blogs)</h5>
</div>

As you can see, a user can choose from 4 options in the select menu. I want to know ,is there any way to change the content of this div => <div class="pagetitle"> with javascript onsubmit ? For example if a user choosed blogs.php ,the h5 tag will change to <h5>blogs</h5> & if he choosed portfolio.php ,it'll be changed to <h5>portfolio</h5> . I really appreciate if you know how to do this ... thanks in advance!

  • 写回答

3条回答 默认 最新

  • dscrn1974 2016-01-25 19:46
    关注

    You need to bind a jquery .change event to the select element and have it's selected value populated as the text of h5 tag

    <script>
        $(document).ready(function(){
        $('select').change(function()
        {
            $('h5').text($(this).val());
        }).change(); // this is optional - it basically invokes the change event as soon as the function is registered.
    });
    </script>
    

    Example : https://jsfiddle.net/DinoMyte/6x80vabm/4/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效