通过提交按钮执行表单操作,具体取决于&#39; <a>&#39; link chosen from another different page using javascript

时间:2015-11-11 07:49:49

标签: javascript html

There are 4 different HTML pages namely: "HomePage.html", "ABCpage.html", "Page1.html" & "Page2.html".

HomePage.html - In this page there are 2 links both when clicked are redirected to 'ABCpage.html'. The following is the HTML code below.

<body>
  <a href="ABCpage.html" id="page1">Page 1</a>
  <a href="ABCpage.html" id="page2">Page 2</a>
</body>

ABCpage.html - In this page there is only an input button named 'Enter', which when clicked should redirect either to "Page1.html" or "Page2.html" depending on the link choosed from "HomePage.html"

<body>
  <form method="post">
    <input type="submit" id="submit" value="Enter" />
  </form>
</body>

"Page1.html" and "Page2.html" are just blank pages.

So what should I do to redirect from "ABCpage.html" to either "Page1.html" or "Page2.html" depending on the links chosen from "HomePage.html". Now I am purely using javascript for client side scripting and JSP for server side scripting and am not familiar with jquery. Now the link that I choose from "HomePage.html" should directly affect the form action in "ABCpage.html" for redirecting it further either to "Page1.html" or "Page2.html" depending on the link chosen. So what could be the javascript code for the following as I am finding it hard to get a solution for this.

2 个答案:

答案 0 :(得分:0)

您需要从第一页传递查询字符串参数。因此,主页上链接上的href值应该更像是这样:

ABCpage.html?page=2 ABCpage.html?page=1

然后在ABCpage.html上,您将需要使用javascript从URL解析查询字符串params并将它们附加到您的表单操作。 JS中的逻辑将读取查询字符串参数,并根据一些基本条件语句确定表单操作。

这是一个在ABC页面上可以用来检索查询字符串参数的函数:

function getParameterByName(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); }

信用:How can I get query string values in JavaScript?

答案 1 :(得分:0)

您需要记住用户选择。有很多方法可以使用cookie,本地存储API或只是通过URL传递它。

最后一个是我眼中最简单的,你可以基本上以两种方式做到:

a)通过将其作为url参数传递,可以读取服务器端,使服务器能够实际服务于不同的页面,即使唯一的差异发生在表单操作URL上。那就是:

  • Link1 = abcPage.html?id = 1
  • Link2 = abcPage.html?id = 2

b)通过使用hashurl,这是不可读的服务器端,然后你真的需要依赖javascript从位置对象中读取它(并在需要时检测它的变化)。

  • Link1 = abcPage.html#1
  • Link2 = abcPage.html#2