我正在使用html和javascript。我的问题是,在一个网页上显示一个情节和几个按钮。当用户按下任何一个按钮时,我需要显示3或4个选项,但在同一页面中没有切换页面。 以下是我的代码
<form action="MyPage">
<button type="submit" value="More Options">
</form>
重定向到另一页。我能做什么?
答案 0 :(得分:1)
首先,摆脱type =&#34;提交&#34;。那是什么导致页面做你不想要的东西。第二件事是添加一个onclick处理程序。它应该返回false以避免像&#34;提交&#34;这样的行为。变量&#39; this&#39;将按钮传递给您在该代码中可能需要的功能。然后用你的代码填充addMoreStuff()的主体,好吧,添加更多东西!
<form action="MyPage">
<button onclick="addMoreStuff(this); return false; ">More Options</button>
</form>
<script type="text/javascript">
function addMoreStuff(button) {
/* your code here */
}
</script>
答案 1 :(得分:-1)
删除表单(单独使用按钮),然后查看jQuery。它非常易于使用,它可以帮助您快速构建应用程序代码。
HTML
<button type="submit" value="More Options" id="more">
JavaScript(jQuery)
// run "add_options" when someone clicks on the button
jQuery('button#more').on('click', add_options)
function add_options() {
//code here to add more options
}